Shell – Printing colored text using script

echoescape-charactersquotingshellshell-script

When I type below command in the shell I get the OUTPUT in green color.

Command

echo "\033[32mCONNECTING TO abpwrk\033[m";

Output(in green color)

CONNECTING TO abpwrk

But if I use the same statement in a small one line script and execute it.Then I get the output(in white color).

\033[32mCONNECTING TO abpwrk\033[m

What am I missing in the script.Do I need to define some extra parameters to initiate colors ? I am using k-shell.

Best Answer

You probably are using different shells with different echo implementations. At least in bash's implementation of echo, interpretation of escape sequences is not enabled by default (you have to enable it with the -e switch).

In any case, printf is generally more conformant between environments. Try using the following instead:

_host=abpwrk ; printf '\033[32mCONNECTING TO %s\033[m\n' "$_host"