Skip to content
Advertisement

Colorizing echo works in Solaris but doesn’t work in Linux

I have 2 different servers where I log in with same network user and run the same script. One is Solaris and another is Linux.

Sample echo line from the script works differently on those servers:

echo  "33[1;32mauto update33[m"

In Solaris -> it displays the text “auto update” correctly in green

In Linux -> it displays the text incorrectly and with no color coding: “33[1;32mauto update33[m”

.bashrc profile has the following line for PS1:

export PS1='${USER}@${HOST%%.*} ${PWD}> '

Grateful if you can help me getting the color thing work in Linux. Thanks

Advertisement

Answer

This is a good illustration of why echo is not considered portable.

On dash and Solaris sh:

echo    "33[1;32mauto update33[m"   # Works
echo -e "33[1;32mauto update33[m"   # Fails (prints -e)

On bash, ash and ksh:

echo    "33[1;32mauto update33[m"   # Fails (doesn't interpret escapes)
echo -e "33[1;32mauto update33[m"   # Works

You can instead use printf:

printf '33[1;32mauto update33[mn'  # Works on all platforms
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement