在linux console显示各种颜色的信息可以使用特殊的符号串:escape sequence code.通过使用escape sequence code可以在linux终端以高亮,粗体,闪烁、多种颜色等方式展示消息
下面以实例说明:
在终端打印hello world
如果加上escape sequence code就可以调整打印在终端上的消息属性:
\033[34m 就是 escape sequence code
\033[34m Hello Colorful World\!整个字符串的处理过程为:
1.读到 \033,说明后面的字符是ANSI escape sequence,会特殊处理
2.使用 [34m 来设定终端的前景色为蓝色
3.打印 Hello Colorful World\! ,颜色为蓝色
\033[ 实际上在输入的文本信息中作为一个特殊标识,终端读到 \033 字符的时候,识别出这是一个escape character;将切换到escape模式。然后读取“[” 字符移入到CSI模式(Command Sequence Introduction (CSI) mode),在CSI模式下,终端读取这些ASCII码用分号“;”隔开,直到读入一个完整的动作指令。上面打印Hello Colorful World!的例子里面
\033 作为escape character, 通知终端切换到escape模式;
[ CSI的开始;
34 读入的参数;
m 制定将要执行的动作
在shell脚本中可能会使用到一些常用的动作及属性,表格表示如下:
Code | Name | Effect |
---|---|---|
CSI n A | CUU – CUrsor Up | Moves the cursor n (default 1) cells in the given direction. If the cursor is already at the edge of the screen, this has no effect. |
CSI n B | CUD – CUrsor Down | |
CSI n C | CUF – CUrsor Forward | |
CSI n D | CUB – CUrsor Back | |
CSI n E | CNL – Cursor Next Line | Moves cursor to beginning of the line n (default 1) lines down. |
CSI n F | CPL – Cursor Previous Line | Moves cursor to beginning of the line n (default 1) lines up. |
CSI n G | CHA – Cursor Horizontal Absolute | Moves the cursor to column n. |
CSI n ; m H | CUP – CUrsor Position | Moves the cursor to row n, column m. The values are 1-based, and default to 1 (top left corner) if omitted. A sequence such as CSI ;5H is a synonym for CSI 1;5H as well as CSI 17;H is the same as CSI 17H and CSI 17;1H |
CSI n J | ED – Erase Data | Clears part of the screen. If n is zero (or missing), clear from cursor to end of screen. If n is one, clear from cursor to beginning of the screen. If n is two, clear entire screen (and moves cursor to upper left on MS-DOS ANSI.SYS). |
CSI n K | EL – Erase in Line | Erases part of the line. If n is zero (or missing), clear from cursor to the end of the line. If n is one, clear from cursor to beginning of the line. If n is two, clear entire line. Cursor position does not change. |
CSI n S | SU – Scroll Up | Scroll whole page up by n (default 1) lines. New lines are added at the bottom. (not ANSI.SYS) |
CSI n T | SD – Scroll Down | Scroll whole page down by n (default 1) lines. New lines are added at the top. (not ANSI.SYS) |
CSI n ; m f | HVP – Horizontal and Vertical Position | Moves the cursor to row n, column m. Both default to 1 if omitted. Same as CUP |
CSI n [;k] m | SGR – Select Graphic Rendition | Sets SGR parameters, including text color. After CSI can be zero or more parameters separated with ;. With no parameters, CSI m is treated as CSI 0 m (reset / normal), which is typical of most of the ANSI escape sequences. |
CSI 6 n | DSR – Device Status Report | Reports the cursor position to the application as (as though typed at the keyboard) ESC[n;mR, where n is the row and mis the column. (May not work on MS-DOS.) |
CSI s | SCP – Save Cursor Position | Saves the cursor position. |
CSI u | RCP – Restore Cursor Position | Restores the cursor position. |
CSI ?25l | DECTCEM | Hides the cursor. (Note: the trailing character is lowercase L.) |
CSI ?25h | DECTCEM | Shows the cursor. |
Code | Effect | Note |
---|---|---|
0 | Reset / Normal | all attributes off |
1 | Bright (increased intensity) or Bold | |
2 | Faint (decreased intensity) | not widely supported |
3 | Italic: on | not widely supported. Sometimes treated as inverse. |
4 | Underline: Single | |
5 | Blink: Slow | less than 150 per minute |
6 | Blink: Rapid | MS-DOS ANSI.SYS; 150 per minute or more; not widely supported |
7 | Image: Negative | inverse or reverse; swap foreground and background |
8 | Conceal | not widely supported |
9 | Crossed-out | Characters legible, but marked for deletion. Not widely supported. |
10 | Primary(default) font | |
11–19 | n-th alternate font | Select the n-th alternate font. 14 being the fourth alternate font, up to 19 being the 9th alternate font. |
20 | Fraktur | hardly ever supported |
21 | Bright/Bold: off or Underline: Double | bold off not widely supported, double underline hardly ever |
22 | Normal color or intensity | neither bright, bold nor faint |
23 | Not italic, not Fraktur | |
24 | Underline: None | not singly or doubly underlined |
25 | Blink: off | |
26 | Reserved | |
27 | Image: Positive | |
28 | Reveal | conceal off |
29 | Not crossed out | |
30–37 | Set text color | 30 + x, where x is from the color table below |
38 | Set xterm-256 text color[dubious ] | next arguments are 5;x where x is color index (0..255) |
39 | Default text color | implementation defined (according to standard) |
40–47 | Set background color | 40 + x, where x is from the color table below |
48 | Set xterm-256 background color | next arguments are 5;x where x is color index (0..255) |
49 | Default background color | implementation defined (according to standard) |
50 | Reserved | |
51 | Framed | |
52 | Encircled | |
53 | Overlined | |
54 | Not framed or encircled | |
55 | Not overlined | |
56–59 | Reserved | |
60 | ideogram underline or right side line | hardly ever supported |
61 | ideogram double underline or double line on the right side | hardly ever supported |
62 | ideogram overline or left side line | hardly ever supported |
63 | ideogram double overline or double line on the left side | hardly ever supported |
64 | ideogram stress marking | hardly ever supported |
90–99 | Set foreground color, high intensity | aixterm (not in standard) |
100–109 | Set background color, high intensity | aixterm (not in standard) |
参考:
1.http://www.cnblogs.com/shipfi/articles/375430.html
2.http://en.wikipedia.org/wiki/ANSI_escape_code#Windows_and_DOS