We have moved to www.dataGenX.net, Keep Learning with us.

Tuesday, April 09, 2013

Colourizing Unix Programs & Shell Scripts

 


In an ANSI compatible terminal (like xterm, rxvt, konsole ...) text may be shown in colors different from black/white. Here first the colour coding is shown for shell programming .

Each color code is present after an escape sequence. This can be given by

echo  –e "\033[1m Demo Test \033[0m\n";





in this \033 is the escape and 1 represents the color code and m is the end of escape .


Color Syntax
echo  "\033[COLORm Sample text"
or
echo  -e "\033[COLORm Sample text"


The "\033[" begins the escape sequence.You can also use "\e[" instead of "\033[". COLOR specifies a foreground color. The "m" terminates escape sequence, and text begins immediately after that.

echo  "\033[40m Sample text"


Here is the color table for other colors that you can lookup and will use frequently.

Color              Foreground             Background
Black                            30                     40
Red                              31                     41
Green                           32                     42
Yellow                           33                     43
blue                              34                     44
magenta                        35                     45
cyan                             36                     46
white                             37                     47


echo  -e "\033[40m Sample text"









The above define command, there was a problem  ( you can see the screenshot ) , Here we set the color for the terminal not for the line. If we want to use it for specific words, we have to return in our old color coding so need to put these chunks of code where we want to end out fancy output.


Return to Normal Syntax.
echo "\033[0m"
or
echo -e "\033[0m"

Now you won't see anything new on the screen, as this echo statement was not passed any string to display. But it has done its job, which was to restore the normal viewing mode. Whatever yor type now will be avoid of any fancy effects.










The following table summarizes numbers representing text attributes in Escape Sequences.

ANSI CODE              Meaning
0                                  Normal Characters
1                                  Bold Characters
4                                  Underlined Characters
5                                  Blinking Characters
7                                  Invert color coding

Blinking Characters is not working with terminal.



a)    Foreground Color change Example

echo  -e "\033[31m   This is Red  \n \033[32m  This is Green \n \033[33m  This is Yellow \n \033[34m  This is blue \n \033[36m  This is cyan  \033[0m";









There are some differences between colors when combining colors with bold text attribute.

The following table summarizes these differences.

Bold off         color                          Bold on         color
0;30                  Black                            1;30                  Dark Gray
0;31                  Red                              1;31                  Dark Red
0;32                  Green                           1;32                  Dark Green
0;33                  Yellow                           1;33                  Dark  Yellow
0;34                  Blue                              1;34                  Dark Blue
0;35                  Magenta                        1;35                  Dark Magenta
0;36                  Cyan                             1;30                  Dark Cyan
0;37                  Light Gray                     1;30                  White



b)    Bold Foreground Color change Example

echo  -e "\033[1;31m   This is Dark Red  \n \033[1;32m  This is Dark Green \n \033[1;33m  This is Yellow \n \033[1;34m  This is Dark blue \n \033[1;36m  This is Dark cyan  \033[0m";










c)    Underlined Foreground Color change Example

echo  -e "\033[4;31m   This is Dark Red  \n \033[4;32m  This is Dark Green \n \033[4;33m  This is Yellow \n \033[4;34m  This is Dark blue \n \033[4;36m  This is Dark cyan  \033[0m";











d)    Underlined Background Color change Example

echo  -e "\033[4;41m   This is Dark Red  \n \033[4;42m  This is Dark Green \n \033[4;43m  This is Yellow \n \033[4;44m  This is Dark blue \n \033[4;46m  This is Dark cyan  \033[0m";










Some More Example :


e)    Red on Green





f)     Green on Red






g)    Blue on Yellow





The semicolon separated numbers "COLOR1" and "COLOR2" specify a foreground and a background color. The order of the numbers does not matter.

Hoping this will help you to write fancy and eye catching scripts.

Feel free to contact if any queries.