ANSIEscape sequencesAre 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 (portable t 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
AboveEchoStatement
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 [34 m Hello colorful world! "
Hello colorful world!
Above echo statement uses ANSI escape sequence (\ 033 [34 m), Abve entire string (I. e."\ 033 [34 m 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[34 mEscape code.
3) then it prints our normal messageHello colorful world!In blue color.
Escape sequences start with the characterESC(ASCII decimal 27/Hex 0x1b/Octal 033). You can useEchoStatement to print message, to use ANSI escape sequence you must use-EOption (switch) with ECHO statement, general syntax is as follows Syntax:
Echo-e "\ 033 [Escape-codeYour-message"
In above syntax you have to use\ 033 [As its with differentEscape-codeFor
Different operations. As soon as console when es 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 movesCommand 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 differentColorsOr effects suchBoldAnd blink, see below for parameter taken by M. |
Echo-e "\ 033 [35 m 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 [7 S" |
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 |
[Transfer] http://lishicongli.blog.163.com/blog/static/14682590201132151848668/