Thursday, December 18, 2008

Linux:shell:Bash: How to change color of your shell printout


You could use 'echo -e' to change your printout text and background color by some color variables. One of the easiest ways to use them is to define shell variable, for example, define those variables in your shell script:

red='\e[0;31m'
RED='\e[1;31m'

blue='\e[0;34m'

BLUE='\e[1;34m'

cyan='\e[0;36m'

CYAN='\e[1;36m'
NC='\e[0m' # No Color


. '\e[0;36m': 0's place is the color of background (0, no effect, 1, bold, 4, underline) and 36 is the text color. You also can change background colors due to color values in the color table. For example, '\e[41;34m' changes background color to red and text color to blue.

How to print?

echo -e "$red These texts are in red$NC" #you must use '-e' to enable interpretation of the backslash-escaped characters, see echo's help.

or if you want to use it in 'printf':

printf "%s\n" "`echo -e $red`These texts are in red`echo -e $NC`"

There is a table of color values attached (this table is from the link). You can find color values what you want.

Locations of visitors to this page