22

Consider this simple example (which displays in red):

echo -e "\033[31mHello World\033[0m"

It displays on the terminal correctly in red. Now consider:

watch echo -e "\033[31mHello World\033[0m"

It does not display the color.

Note: I am aware that it is easy to write a loop that mimics the basic behavior by clearing and rerunning. However, the clear operation causes the screen to flash, which does not happen under watch

EDIT: Originally this question specified escape sequences rather than vt100 sequences, but that is not really what I am after, and was solved with single quotes.

frankc
  • 11,290
  • 4
  • 32
  • 49
  • 1
    By using only shell builtins to clear and display the content, it technically won't flicker. Save the content into a shell variable, then `clear` the screen and `echo` the var: `while sleep – Géza Török Sep 14 '17 at 13:34

3 Answers3

13

From man watch of watch 0.3.0 on Ubuntu 11.10:

By default watch will normally not pass escape characters, however if you use the --c or --color option, then watch will interpret ANSI color sequences for the foreground.

It doesn't seem to work with your literal string on my terminal, but these work:

watch --color 'tput setaf 1; echo foo'
watch --color ls -l --color
l0b0
  • 55,365
  • 30
  • 138
  • 223
  • Its in the manpage for watch `man watch` on mac osx, but afaik it doesnt work . – Mike Graf Jul 02 '13 at 23:37
  • This is giving me a headache! has colorizing codes scattered through out. `tail -n 5 ` shows me a beautiful, colorful display. `watch tail -n 5 ` shows me b/w output and I can see all the codes. `watch -c tail -n 5 ` shows me b/w but none of the codes (which makes it easier to read, but I wanted color). – MACE Sep 28 '20 at 01:28
  • It turns out my prior comment is moot, `tail` has an `-f` parameter that incorporates a `watch` feature that retains and colorfully displays any color codes that are contained in the file. – MACE Sep 28 '20 at 21:25
7

Edit:

More recent versions of watch support color. You will need to use an extra level of quoting to preserve the quotes and escapes in the particular situation of the example in the question:

watch 'echo -e "\033[31mHello World\033[0m"'

From man watch:

  -c, --color
          Interpret ANSI color sequences.

Previously:

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.

But they don't get interpreted, so I don't think there's any way.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • 1
    I'm basically convinced it's impossible. – frankc Apr 01 '10 at 16:22
  • I believe a recent version of watch may have added color support, but I haven't located documentation. – Dennis Williamson Jan 24 '12 at 19:37
  • 1
    @AmanuelNega: It works for me. I tried `watch git status` and `watch git log`. You didn't say which subcommand you tried or how exactly it didn't work for you so there's no way I can guess what your problem is. If you mean that you're not getting color then you need to tell `git` to output color when its output is not to the tty: `watch --color git log --color`. This is a typical behavior (try `ls | cat` vs. `ls --color | cat`, for example. – Dennis Williamson Jul 31 '17 at 13:16
  • Thanks I figured it out. I changed the config to always use color – Amanuel Nega Jul 31 '17 at 14:09
1

You can try single quoting your command :

watch 'echo -e "\tHello World"'

On my machine this leaves me with a -e as first character, and a correctly tabbed hello world. It seems -e is the default for my version of echo. Still, it is a progress toward a correctly tabbed hello world

What happens is a double unquoting :
what watch see

echo -e "\033[31mHello World\033[0m"

what the shell called by watch see :

echo -e \033[31mHello World\033[0m

And then the backslash come into play, even when quoted, and it becomes a quoting nightmare.

shodanex
  • 14,975
  • 11
  • 57
  • 91
  • In an attempt to simplify things, I have probably made things too simple. You are right that this works and I have upvoted you for that, but what I am really after is getting vt100 codes to work correctly, which appears to be a different problem than just getting escape sequences interpreted. For instance: echo -e "\033[31mHello World\033[0m" does not work properly under watch even with single quotes. – frankc Mar 10 '10 at 17:10