Skip to content
Advertisement

Show colored output in terminal when using the sh command

color.sh

 RED='u001b[31m'
 GREEN='u001b[32m'
 BLUE='u001b[34m'
 YELLOW='u001b[33m'
 WHITE='u001b[37m'

 # use echo -e to print in certain colors
 echo -e ${RED} RED
 echo -e ${YELLOW} YELLOW
 echo -e ${GREEN} GREEN
 echo -e ${BLUE} BLUE
 echo -e ${WHITE} WHITE

When typing $ ./color.sh the terminal outputs in the respective colors. However, when I type $ sh color.sh it does not output in the correct colors. Instead it gives me the output below.

-e u001b[31m RED
-e u001b[33m YELLOW
-e u001b[32m GREEN
-e u001b[34m BLUE
-e u001b[37m WHITE

Is there any way to make $ sh color.sh print in the respective colors? Thanks.

Advertisement

Answer

Use this codes

 RED='e[31m'
 GREEN='e[32m'
 BLUE='e[34m'
 YELLOW='e[33m'
 WHITE='e[37m'

But better use printf

printf "${RED} REDn"
printf "${YELLOW} YELLOWn"
printf "${GREEN} GREENn"
printf "${BLUE} BLUEn"
printf "${WHITE} WHITEn"

Also take a look here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement