bash watch command with colors preserved

65

15

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

Paweł Gościcki

Posted 2010-03-29T19:00:04.377

Reputation: 1 949

Answers

74

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.

paperhorse

Posted 2010-03-29T19:00:04.377

Reputation: 864

How do you update your watch version? – Adam Hunyadi – 2017-05-19T20:47:16.327

@PawełGościcki Nevermind, already found it on gitorious, although, couldn't get it to work with phpunit. – Ikke – 2012-01-19T13:11:32.893

1My watch is v0.3.0 and I'm on Ubuntu 10.0 – Paweł Gościcki – 2012-01-19T14:00:06.800

2I've also version 0.3.0 and for simple "ls --color" the watch command will work but for some reason more complex scenarios do not: watch --color "sudo iwlist wlan0 scanning | egrep 'Quality|ESSID' | egrep --color -i 'foobar|$'"will eat the colors :( – math – 2012-03-07T13:39:57.690

@math: Try swapping your single and double quotes. – Paused until further notice. – 2012-04-11T11:03:03.687

The watch from procps (the default on most Linux distros, I believe) has a --color option since V3.3.2. – sleske – 2013-09-04T22:43:24.727

Note that the version 3.3.2 is only available in procps-ng, original procps.sf.net seems development has stopped years ago (version is still 3.2.8 and watch version 0.2.0). Also note to OSX/Darwin users the new and maintained fork of procps-ng doesn't yet compile on OSX using LLVM (see https://github.com/Homebrew/homebrew/pull/20845).

– Brice – 2014-01-07T16:07:44.863

27

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.

davr

Posted 2010-03-29T19:00:04.377

Reputation: 4 809

man watch clearly suggests that it should be possible without dissing watch. – Paweł Gościcki – 2010-03-29T19:27:46.650

It doesn't say you will be able to see colors. It says you will be able to see the non-printable characters. Try my command as above with cat -v to see what man watch was talking about. – davr – 2010-03-29T20:03:53.460

OK. It seems I must accept the fact that it's simply impossible. – Paweł Gościcki – 2010-03-31T15:37:57.553

Is there a way to suppress the blinking that results from the cycles of clear and print operations, a behavior not present in the watch command? – user001 – 2016-10-01T02:43:39.300

1You can reduce the duration of the blink a little by a) collecting the data to be displayed into a variable, b) clearing the screen, c) printing the variable. – Nick Russo – 2016-12-13T18:21:59.780

1@NickRusso thanks for the suggestion. Something like this greatly reduces the flickering: while true; do out=$(date;echo;ls -al --color);clear;echo $out;sleep 2;done – Kevin Mark – 2017-07-23T14:21:57.383

Also consider trimming line length to fit in terminal: https://superuser.com/a/58812/114579

– nobar – 2017-09-15T21:18:26.327

1

@KevinMark: You should use quotes to handle multiple lines: echo "$out". https://stackoverflow.com/q/2414150/86967

– nobar – 2017-09-15T21:20:37.083

@nobar You're right. Looks like it needs quotes in bash not not zsh. – Kevin Mark – 2017-09-15T21:27:04.283

@KevinMark You can put the "clear" command inside the parentheses. It just prints a terminal control code. See also my longer answer https://superuser.com/a/1270584/21952.

– user21952-is-a-great-name – 2017-11-21T20:57:43.253

6

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.

fatuhoku

Posted 2010-03-29T19:00:04.377

Reputation: 232

fswatch is available on Homebrew 0.9.5 – code_monk – 2014-10-20T01:45:09.290

2This only works for the filesystem, while watch applies to a command – Brice – 2014-01-07T15:49:49.287

1

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'

user21952-is-a-great-name

Posted 2010-03-29T19:00:04.377

Reputation: 171