'watch' not interpreting escape codes in bash script

Hi there,

I'm fairly new to bash scripting and already having some troubles. I'm making a script that can print some series of strings in colors based in the information of a file, for simplicity let's say it only does:

#!/bin/bash
printf "\e[1;31;32m%-10s\e[00m" "OK"

When you execute this in the command line it prints a bold green 'OK'. So far so good.

Now, I need to check the output of the script over time using the command watch. The problem then arises. watch seems to ignore the escape codes and just prints:

[1;31;32mOK [00m

Is there any way to fix this?

If not, how can I check inside the script if it is being executed from a command? (watch in this case) So I can print without color for those cases.

Thanks for your time in advance!

Hi.

I assume you are asking what happened to the escape sequence "\e" when you supply the command directly to watch.

I think the command is being scanned by the shell twice, once on the command line when you give it to watch, and again when watch executes it. If so, then the "\" will disappear, although I would have expected the "e" to remain.

You can test this -- suppose you use your shell script (the 2-liner with bash) as the command in watch. What happens then? ... cheers, drl