翻阅了很多中文的博文,多数文章是讲echo颜色的用法,本人实在不爱死背,追本溯源,真正看看转义序列是什么?

转义字符,学习过C语言的童鞋都知道,著名的printf函数中支持一些控制字符输出,例如\t 、\n等。我们通常称它们为转义字符。

一、转义序列是什么?

1、维基百科(译者怕翻译偏差大,保留原文)    
In computing, ANSI escape code (or escape sequences) is the method of in-band signaling to control formatting, color, and other output options on video text terminals.    
在计算机界,ANSI转义编码或者转义序列是带内信号(元数据和控制信号,译者注)控制视频文字终端的格式、颜色、其他输出选项的一种方法。    
To encode this formatting information, it embeds certain sequences of bytes into the text, which have to be interpreted specially, not as codes of characters.    
为了编码这些格式信息,它把某种字节的序列嵌入到文本中,该序列必须特别的解释,且不应该作为字符的本义。


Although hardware text terminals have become increasingly rare in the 21st century, the relevance of this standard persists because most terminal emulators interpret at least some of the ANSI escape sequences in the output text.  
尽管硬件字符终端在21世纪已经变得很愈来愈少见,这种标准依然具有实用价值,是因为大多数终端仿真器至少还能解释在输出文本中的一些ANSI转义序列。  
One notable exception is the win32 console component of Microsoft Windows.  
但微软的win32控制台组件就是一个明显的例外。


2、微软的解释    
包含反斜杠 (\) 的字符组合后跟字母或用数字的组合称为 “转义序列”。若要显示换行符、单引号或某些其他字符常数的,必须使用转义序列。 因此转义序列被视为单个字符是有效的作为字符常数。  

二、转义序列定义    
Sequence elements    
序列元素    
Escape sequences start with the character ESC (ASCII decimal 27/hex 0x1B/octal 033).    
转义序列使用ESC控制字符开始。    
For two character sequences, the second character is in the range ASCII 64 to 95 (@ to _).    
对于2个字符序列,第二个字符是ASCII的64到95。(@到_,还有所有大写英文字母和[]\^)    
However, most of the sequences are more than two characters, and start with the characters ESC and [ (left bracket).    
然而,序列中大多数是超过2个字符的,并以ESC控制字符和左中括号开始。    
This sequence is called CSI for Control Sequence Introducer (or Control Sequence Initiator). The final character of these sequences is in the range ASCII 64 to 126 (@ to ~).    
序列被称作CSI,即控制序列引导器或者控制序列启动器的简称。这个序列的最后一个字符是在ASCII范围64到126。    
There is a single-character CSI (155/0x9B/0233) as well.    
也还有一个单字符的CSI (155/0x9B/0233)。    

The ESC+[ two-character sequence is more often used than the single-character alternative, for details see C0 and C1 control codes.    
ESC[,这两个字符序列比单个字符形式用的更多,细节参看C0和C1控制编码。    
Only the two-character sequence is recognized by devices that support just ASCII (7-bit bytes) or devices that support 8-bit bytes but use the 0x80–0x9F control character range for other purposes.    
那些 仅支持ASCII(7位字节),或者支持8位字节但出于其他用途而使用0x80-0x9F控制字符区域的设备 仅能识别 2字符的序列。    
On terminals that use UTF-8 encoding, both forms take 2 bytes (CSI in UTF-8 is 0xC2, 0x9B) but the ESC+[ sequence is clearer.    
使用UTF-8编码的终端上,两种形式都使用2字节(CSI in UTF-8 is 0xC2, 0x9B),但ESC[序列更加明了。

Though some encodings use multiple bytes per character, the following discussion is restricted to ASCII characters, and so assumes each character is directly represented by a single byte.    
虽然一些编码使用多字节字符,下面的讨论仅针对ASCII字符,因此假定每一个字符由单个字节表示。

30–37    Set text color (foreground)    30 + x, where x is from the color table below    
38    Set xterm-256 text color (foreground)[dubious – discuss]    next arguments are 5;x where x is color index (0..255)    
39    Default text color (foreground)    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)

三、颜色控制序列    
Colors    
Text colors (and SGR parameters in general) are manipulated using CSI n1 [;n2 [; ...]] m sequences, where each n1, n2, ... is an SGR parameter as shown above.    
文本颜色(和SGR (Select Graphic Rendition)参数)使用CSI n1 [;n2 [; ...]] m 序列来处理,如上所示,序列中每一个n1, n2, ...就是一个SGR参数。    
Thus, for instance, you use codes 30+i to specify foreground color, 40+i to specify background color, where i is the number in the desired color's column header in the table below.    
因此,例如,你使用30+i表示前景色,40+i表示背景色, i 就是下表中的颜色数字。

Color table颜色表
Intensity 0 1 2 3 4 5 6 7
Normal Black Red Green Yellow Blue Magenta Cyan White
Bright Black Red Green Yellow Blue Magenta Cyan White

The following examples can be used with the printf utility, where \x1b[ implements the CSI: To switch the foreground color to black, use \x1b[30m; to switch to red, use \x1b[31m; utilizing the "bold" parameter, gray would be \x1b[30;1m; to get bold red, use \x1b[31;1m. To reset colors to their defaults, use \x1b[39;49m (or reset all attributes with \x1b[0m).    
下面的例子使用printf功能,在其中 \x1b[ 实现CSI:    
转换前景色为黑色,使用\x1b[30m    
转换为红色,使用\x1b[31m    
如果使用加粗参数,灰色写作\x1b[30;1m    
获取红色加粗,使用\x1b[31;1m    
重置颜色为缺省值,使用\x1b[39;49m    (或者使用 \x1b[0m 重置所有属性)

\033[0m 重置为正常    
\033[1m 设置高亮度或加粗    
\033[4m 下划线    
\033[5m 闪烁    
\033[7m 反显    
\033[8m 消隐    
\033[30m -- /33[37m 设置前景色    
\033[40m -- /33[47m 设置背景色

控制符ESC的常用表示方法\e、\x1b(\x1B)、\033都可以    
\e 指代Escape,对应八进制\033,对应十六进制\x1b

四、举例:    
使用CentOS 6 的echo命令 
1、$ echo -e "\033[1;31;42mMine is red \e[0m" 
高亮绿底红字    
2、$ echo -e "\033[4mm is red" 
下划线显示,但没有关闭,下划线一直存在,直到遇到第3个示例    
3、$ echo -e "\x1b[0m"    
关闭所有属性,恢复正常


特别感谢,老婆在翻译上给出的大力支持及好的建议。


参看文献    
http://en.wikipedia.org/wiki/ANSI_escape_code