3

all.

I have the following exemple code:

#!/bin/bash
echo "[\033[01;32m  green  \033[01;37m]"
echo "[\033[01;31m  red  \033[01;37m]"

When I run this script locally, I get the correct output color.

But if I copy to another host over scp and run it with "ssh avadmin@10.224.1.6 "sudo sh color-test.sh"" the script will print the code instead of the color.

Should I have any considerations when running script remotely?

4

The behaviour of

echo '\033'

Depends on the implementation and/or the environment.

With UNIX-compliant implementations, it outputs a ESC character followed by NL, in some others, it outputs \033 followed by NL. Some support a -e option for those \033 escape sequences to be expanded.

Your script has a #! /bin/bash she-bang, but you're running the script as sh the-script, so sh interprets it, not bash.

On most systems, bash's echo builtin doesn't expand \x sequences by default. It only does so when the xpg_echo option is enabled or by using the -e option (unless both the xpg_echo and posix options have been enabled).

Probably your local sh implementation is a UNIX-compliant one in that regard (like dash, or bash compiled with the xpg_echo option enabled by default), but the one on the remote server is not (like bash).

If you want a portable and reliable behaviour, use printf instead:

printf '[\33[01;32m  green  \33[01;37m]\n'
printf '[\33[01;31m  red  \33[01;37m]\n'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.