From man watch:
Non-printing characters are stripped from program output. Use "cat -v" as part of the command pipeline if you want to see them.
So how do I use cat -v if I want to see the colored output from:
watch ls -al --color
From man watch:
Non-printing characters are stripped from program output. Use "cat -v" as part of the command pipeline if you want to see them.
So how do I use cat -v if I want to see the colored output from:
watch ls -al --color
The right command is
watch --color "ls -a1 --color"
It isn't documented in the man page or the --help screen. I has to use strings to find it.
I think it may not be possible with the 'watch' command. Here is a longer way of doing it:
while true; do clear; date;echo;ls -al --color; sleep 2; done
You could put this in a script, for example:
echo "while true; do clear; date;echo;\$*;sleep 2; done" > watch2
chmod +x watch2
./watch2 ls -al --color
To clarify, here's why I think it's not possible with the 'watch' command. See what happens if you use cat -v:
watch "ls -al --color|cat -v"
It shows you the color control characters...which I think is not what you want.
man watch clearly suggests that it should be possible without dissing watch.
– Paweł Gościcki
Mar 29 '10 at 19:27
cat -v to see what man watch was talking about.
– davr
Mar 29 '10 at 20:03
watch command?
– user001
Oct 01 '16 at 02:43
while true; do out=$(date;echo;ls -al --color);clear;echo $out;sleep 2;done
– Kevin Mark
Jul 23 '17 at 14:21
echo "$out". https://stackoverflow.com/q/2414150/86967
– Brent Bradburn
Sep 15 '17 at 21:20
gh pr status to monitor checks status from the command line.
– ryuzakyl
Aug 14 '23 at 17:32
If you're using a Mac, like me, watch from Homebrew does not support colour.
What you want is fswatch but it's not Homebrew yet. To install it you'll want to do the slightly more convoluted
https://raw.github.com/mlevin2/homebrew/116b43eaef08d89054c2f43579113b37b4a2abd3/Library/Formula/fswatch.rb
See this SO answer for usage.
--color flag mentioned in this answer. watch --version => watch from procps-ng 3.3.16.
– M. Justin
Apr 24 '20 at 17:22
UPDATE: Turns out the latest versions of watch fixed the problem. So, if the colors of watch --color are wrong, it's probably better to just update it (on my system, it's in the procps package).
The color support in watch --color is limited in my experience (though sufficient for ls -l --color). Here's my version of @davr's answer with some extra features, most importantly reduced flicker. You can put it in your .bashrc and use it as cwatch ls -l --color.
# `refresh cmd` executes clears the terminal and prints
# the output of `cmd` in it.
function refresh {
tput clear || exit 2; # Clear screen. Almost same as echo -en '\033[2J';
bash -ic "$@";
}
# Like watch, but with color
function cwatch {
while true; do
CMD="$@";
# Cache output to prevent flicker. Assigning to variable
# also removes trailing newline.
output=`refresh "$CMD"`;
# Exit if ^C was pressed while command was executing or there was an error.
exitcode=$?; [ $exitcode -ne 0 ] && exit $exitcode
printf '%s' "$output"; # Almost the same as echo $output
sleep 1;
done;
}
You can also try things like
cwatch 'ls -l --color | head -n `tput lines`'
if your terminal has fewer lines than the output. That only works if all the lines are shorter than the terminal width, though. The best workaround I know for that is:
cwatch 'let lines=`tput lines`-2; ls -l --color | head -n $lines'
watch --color "sudo iwlist wlan0 scanning | egrep 'Quality|ESSID' | egrep --color -i 'foobar|$'"will eat the colors :( – math Mar 07 '12 at 13:39watchfrom procps (the default on most Linux distros, I believe) has a--coloroption since V3.3.2. – sleske Sep 04 '13 at 22:43watch --version=>watch from procps-ng 3.3.16).watch --help=>-c, --color interpret ANSI color and style sequences. Guessing they must have updated the documentation in newer versions. – M. Justin Apr 24 '20 at 17:18wgcommand, usewatch --color WG_COLOR_MODE=always wg show– Abdull Jan 03 '24 at 07:01