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.)
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
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With