Watch Live Command Output Without Losing the Shell Boundary
You keep mashing the up arrow to re-run a status check, and there is a command built for exactly that: watch. It reruns a command on screen, highlights what changed, and stops when you want it to. The examples use the installed procps-ng 4.0.4 from the procps package.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need a terminal and a command whose output is safe to repeat. No elevated privileges are needed for these examples.
1. Confirm the installed command
Check which binary will run and record its version:
$ command -v watch
/usr/bin/watch
$ watch --version
watch from procps-ng 4.0.4
Your path can differ. The useful part is that the command is available and reports the procps-ng version. This guide follows that installed version and its local manual page.
2. Run a harmless repeating command
By default, watch runs its command every two seconds and keeps running until you interrupt it. Start with a command that only prints the current time:
$ watch -n 1 'date +%T'
The terminal switches to a full-screen display. A header shows the interval, command and current time, and the time should change once per second. Press Ctrl-C to return to your shell.
The quotes matter here. The command is normally passed to sh -c, so the quoted text is one command string and its + sign reaches date unchanged. A command run by watch is not a daemon or a background job: it is repeatedly started in the current terminal display.
Checkpoint
A one-second interval, the output replacing the previous screenful each time, and Ctrl-C stopping the display cleanly without touching any files or services.
3. Watch a real, read-only status command
For a directory view, use ls and ask watch to highlight differences between updates:
$ watch -d -n 2 'ls -l --time-style=long-iso /tmp'
The highlighted lines show changes between successive outputs. This command only reads directory entries. The /tmp directory may be busy, so different machines will show different results.
Use -d=permanent when you want changes to remain highlighted for the lifetime of the display, rather than only highlighting the latest difference:
$ watch -d=permanent -n 2 'ls -l --time-style=long-iso /tmp'
Tip
Do not treat highlighting as a file audit. It compares rendered output, so a reordered listing or a changing timestamp can look like a content change even when nothing real did.
4. Put options before the command
watch uses POSIX option processing. Once it reaches the first non-option argument, later flags belong to the command and are no longer interpreted by watch. Put the interval and display options first:
$ watch -n 5 -t 'printf "checked: "; date +%T'
The -t option removes the header and its blank line. The nested double quotes are consumed by the shell used for the watched command, while the outer single quotes protect the whole command from your interactive shell.
This is a common trap:
$ watch 'printf ready' -n 1
Here -n 1 is passed as part of the command after printf ready; it does not set watch's interval. If a command needs several arguments or shell operators, quote the complete command after all watch options.
5. Choose shell parsing or direct execution
The default shell mode is useful for pipes, redirection, variables and conditional commands:
$ watch -n 2 'printf "files: "; find /tmp -maxdepth 1 -type f -printf "x\n" | wc -l'
The pipe is interpreted by sh -c. Keep the complete pipeline in single quotes so your interactive shell does not run it before watch starts.
Use -x when you want watch to pass the command to exec(2) instead of sh -c. This avoids shell quoting, but shell syntax such as pipes and redirection will not be available. For example:
$ watch -x -n 2 printf '%s\n' 'direct execution'
Choose one mode deliberately. A command that works in shell mode may stop working with -x if it relies on shell syntax.
6. Stop on a useful condition
Several options turn a passive display into a small observation loop. -g exits when the command output changes:
$ watch -g -n 1 'date +%S'
Because the seconds value changes, this should exit on the next different output. The normal successful exit status is zero. Use this for a simple wait until a displayed value changes, then inspect the status in the next shell command.
-e freezes the display when the watched command returns an error and exits after a key press:
$ watch -e -n 2 'test -r /tmp/example-file'
If the file is absent or unreadable, the display stops at the failing result. Press a key to leave the display. This does not create the file and does not repair its permissions.
-q 3 exits after the output stays unchanged for three cycles. For a stable command, this gives a bounded wait instead of an endless display:
$ watch -q 3 -n 1 'printf ready'
Use echo $? immediately afterwards if a script needs to record the result. For scripts that must distinguish an unavailable command, a failed command and a changed display, test those cases separately: watch has several documented exit statuses and can also propagate a child command's status.
7. Keep timing expectations realistic
-n accepts intervals down to 0.1 seconds; smaller values are converted to that minimum. Ordinary mode waits around the requested interval after each cycle, so a slow command makes the displayed cycle take longer. -p asks watch to aim for the same clock positions on each run:
$ watch -p -n 10 'date +%T'
Precision mode cannot make a command that takes longer than ten seconds finish instantly, and the manual also warns that a slow command can lead to rapid catch-up executions. Do not use a tight interval for an expensive command, a command that contacts a slow network service, or one that changes state each time.
The WATCH_INTERVAL environment variable can provide a persistent non-default interval for this program. Prefer an explicit -n in a documented command so a hidden environment setting cannot change its polling rate.
Common traps
- Output seems truncated:
watchshows the first screenful, and terminal width and height limit what fits. - Colours are missing: use
-cfor ANSI colour and style sequences, or-Cto force them off. - Long lines wrap unexpectedly: use
-wto disable wrapping, which truncates rather than wraps them. - Resize output looks stale: the manual says repainting waits until the next scheduled update. The
-roption prevents a resize from rerunning the command, but output may not reappear until the next regular run. - Non-printing characters disappear: add
cat -vin the command pipeline when you need to inspect them.
Warning
Before watching a command that writes files, sends mail, restarts a service or changes system state, run it once manually and confirm that repeating it is safe. watch has no general protection against repeating a destructive command. Stop it with Ctrl-C; if a child process does not end cleanly, use the process-management procedure appropriate to that command rather than repeatedly starting another copy.
Done means
- Version confirmed: you know the installed procps-ng version and that the default interval is two seconds.
- Loop controlled: you can run and stop a read-only refresh loop with
Ctrl-C. - Quoting understood: you put
watchoptions before the command and know when shell quoting is involved. - Exit condition chosen: you selected
-d,-g,-eor-qfor a specific observation condition. - Safety checked: you confirmed the command is safe to repeat before applying
watchto it.