escape sequence code in Linux Shell Programing
How can I write colorful message on Linux Console? , mostly this kind of question is asked by newcomers (Specially those who are learning shell programming!). As you know in Linux everything is considered as a file, our console is one of such special file. You can write special character sequences to console, which control every aspects of the console like Colors on screen, Bold or Blinking text effects, clearing the screen, showing text boxes etc. For this purpose we have to use special code called escape sequence code. Our Linux console is based on the DEC VT100 serial terminals which support ANSI escape sequence code.
What is special character sequence and how to write it to Console?
By default what ever you send to console it is printed as its. For e.g. consider following echo statement,
$ echo "Hello World"
Hello World
Above echo statement prints sequence of character on screen, but if there is any special escape sequence (control character) in sequence , then first some action is taken according to escape sequence (or control character) and then normal character is printed on console. For e.g. following echo command prints message in Blue color on console
$ echo -e "33[34m Hello Colorful World!"
Hello Colorful World!
Above echo statement uses ANSI escape sequence (33[34m), above entire string ( i.e. "33[34m Hello Colorful World!" ) is process as follows
1) First 33, is escape character, which causes to take some action
2) Here it set screen foreground color to Blue using [34m escape code.
3) Then it prints our normal message Hello Colorful World! in blue color.
Note that ANSI escape sequence begins with 33 (Octal value) which is represented as ^[ in termcap and terminfo files of terminals and documentation.
You can use echo statement to print message, to use ANSI escape sequence you must use -e option (switch) with echo statement, general syntax is as follows
Syntax
echo -e "33[escape-code your-message"
In above syntax you have to use33[ as its with different escape-code for different operations. As soon as console receives the message it start to process/read it, and if it found escape character (33) it moves to escape mode, then it read "[" character and moves into Command Sequence Introduction (CSI) mode. In CSI mode console reads a series of ASCII-coded decimal numbers (know as parameter) which are separated by semicolon (;) . This numbers are read until console action letter or character is not found (which determines what action to take). In above example
33 | Escape character |
[ | Start of CSI |
34 | 34 is parameter |
m | m is letter (specifies action) |
Following table show important list of such escape-code/action letter or character
Character or letter | Use in CSI | Examples |
h | Set the ANSI mode | echo -e "33[h" |
l | Clears the ANSI mode | echo -e "33[l" |
m | Useful to show characters in different colors or effects such as BOLD and Blink, see below for parameter taken by m. | echo -e "33[35m Hello World" |
q | Turns keyboard num lock, caps lock, scroll lock LED on or off, see below. | echo -e "33[2q" |
s | Stores the current cursor x,y position (col , row position) and attributes | echo -e "33[7s" |
u | Restores cursor position and attributes | echo -e "33[8u" |
m understand following parameters
Parameter | Meaning | Example |
0 | Sets default color scheme (White foreground and Black background), normal intensity, no blinking etc. | |
1 | Set BOLD intensity | $ echo -e "I am 33[1m BOLD 33[0m Person" I am BOLD Person Prints BOLD word in bold intensity and next ANSI Sequence remove bold effect (33[0m) |
2 | Set dim intensity | $ echo -e "33[1m BOLD 33[2m DIM 33[0m" |
5 | Blink Effect | $ echo -e "33[5m Flash! 33[0m" |
7 | Reverse video effect i.e. Black foreground and white background in default color scheme | $ echo -e "33[7m Linux OS! Best OS!! 33[0m" |
11 | Shows special control character as graphics character. For e.g. Before issuing this command press alt key (hold down it) from numeric key pad press 178 and leave both key; nothing will be printed. Now give –> command shown in example and try the above, it works. (Hey you must know extended ASCII Character for this!!!) | $ press alt + 178 $ echo -e "33[11m" $ press alt + 178 $ echo -e "33[0m" $ press alt + 178 |
25 | Removes/disables blink effect | |
27 | Removes/disables reverse effect | |
30 – 37 | Set foreground color 31 – RED 32 – Green xx – Try to find yourself this left as exercise for you 🙂 | $ echo -e "33[31m I am in Red" |
40 – 47 | Set background color xx – Try to find yourself this left as exercise for you 🙂 | $ echo -e "33[44m Wow!!!" |
q understand following parameters
Parameters | Meaning |
0 | Turns off all LEDs on Keyboard |
1 | Scroll lock LED on and others off |
2 | Num lock LED on and others off |
3 | Caps lock LED on and others off |
Click here to see example of q command.
Click here to see example of m command.