Question

Possible Duplicate:
Colors with unix command “watch”?

In one of my programs, I want to use colored text as output, so I am using ANSI escape sequences for that, e.g. like this:

echo -e '\033[34mHello World\033[0m'

It prints "Hello World" in blue color. (actually it is a Python program using "print", but that does not matter for the question)

Now I want to repeat execution of the program using the bash program "watch". But when I execute the very same line as above using "watch", i.e.

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

the text is not blue, and the output becomes "-e 033[34mHello World033[0m". So the ANSI escape sequences are not decoded somehow.

Is there a way to make the program "watch" respect the escape sequences, or is there an alternative to "watch" to obtain the same effect?

It is ok if it only works on Linux as I am only using that.

Update: So one solution would be to install a newer version of "watch" that knows the --color option. But is there any other way, maybe a similar program to "watch"? (I am not the admin of that machine.)

Was it helpful?

Solution

The --color option does the trick for me.

watch --color -n 1 "echo -e '\033[36mHello World\033[0m'"

Or, in the absence of the color option, how about a home-grown watch:

while [ 1 ]; do clear; echo -e '\033[36mHello World\033[0m'; sleep 1; done

OTHER TIPS

Based on ddoxey's answer, I wrote a small script "cwatch" which solves my problem for the moment:

#!/bin/bash
# do something similar to "watch", but enable ANSI escape sequences for colors
# obsolete if new version of "watch" is available which has the --color option
# 2012-11-30

while [ 1 ]
do
  output=$(eval $@)
  clear
  echo -e "$output"
  sleep 2
done

I could still add a "--delay" option...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top