ANSI escape sequences are characters embedded in the text used to control formatting, color, and other output options on video text terminals. Almost all terminal emulators designed to show text output from a remote computer, and (except for Windows) to show text output from local software, interpret at least some of the ANSI escape sequences.
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 "\033[34m Hello Colorful World!"
Hello Colorful World!
Above echo statement uses ANSI escape sequence (\033[34m), above entire string ( i.e. "\033[34m Hello Colorful World!" ) is process as follows
1) First \033, 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.
Escape sequences start with the character ESC (ASCII decimal 27/hex 0x1B/octal 033). 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 "\033[escape-code your-message"
In above syntax you have to use\033[ 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 (\033) 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).
Character or letter |
Use in CSI |
Examples |
h |
Set the ANSI mode |
echo -e "\033[h" |
l |
Clears the ANSI mode |
echo -e "\033[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 "\033[35m Hello World" |
q |
Turns keyboard num lock, caps lock, scroll lock LED on or off, see below. |
echo -e "\033[2q" |
s |
Stores the current cursor x,y position (col , row position) and attributes |
echo -e "\033[7s" |
u |
Restores cursor position and attributes |
echo -e "\033[8u" |
And m understood following parameters:
0 |
reset all attributes to their defaults |
1 |
set bold |
2 |
set half-bright (simulated with color on a color display) |
4 |
set underscore (simulated with color on a color display)(the colors used to simulate dim or underline are set using ESC ] ...) |
5 |
set blink |
7 |
set reverse video |
10 |
reset selected mapping, display control flag, and toggle meta flag (ECMA-48 says "primary font"). |
11 |
select null mapping, set display control flag, reset toggle meta flag (ECMA-48 says "first alternate font"). |
12 |
select null mapping, set display control flag, set toggle meta flag (ECMA-48 says "second alternate font"). The toggle meta flag causes the high bit of a byte to be toggled before the mapping table translation is done. |
21 |
set normal intensity (ECMA-48 says "doubly underlined") |
22 |
set normal intensity |
24 |
underline off |
25 |
blink off |
27 |
reverse video off |
30 |
set black foreground |
31 |
set red foreground |
32 |
set green foreground |
33 |
set brown foreground |
34 |
set blue foreground |
35 |
set magenta foreground |
36 |
set cyan foreground |
37 |
set white foreground |
38 |
set underscore on, set default foreground color |
39 |
set underscore off, set default foreground color |
40 |
set black background |
41 |
set red background |
42 |
set green background |
43 |
set brown background |
44 |
set blue background |
45 |
set magenta background |
46 |
set cyan background |
47 |
set white background |
49 |
set default background color |
[转] http://lishicongli.blog.163.com/blog/static/14682590201132151848668/