<Computer knowledge>: Detailed explanation of ANSI escape sequences and output color characters

I was relatively unfamiliar with this kind of knowledge before, and only knew some fur. Today I checked some information and introduced it. Now I have a certain understanding of ANSI and CSI.

The origin of escape characters

The real hardware terminal device is composed of a keyboard and a display, and is used to communicate with a remote host (computer). Apart from being far away from the host computer (in the next room or in other cities), its use is no different from a personal computer. The program is executed on the host, but the result is displayed on the terminal screen. The terminal is usually limited to displaying the received information and inputting the information sent to the host on the keyboard.
Terminal devices with a single function are more popular in the 1970s and 1980s. People write programs, run programs, write documents, or issue print commands on it. The terminal uses a cable to connect to the host, and sometimes it needs to be connected to the host beyond a long distance through the modem.

There are very few actual terminal devices used today. People usually use software to emulate a personal computer as a terminal connected to the host. Now almost everyone who uses Linux uses the terminal emulation method. For the case where X window is not used, people use terminals with character interfaces (virtual terminals). This is often referred to as the command line interface. In the X window environment, we can obtain multiple terminal windows, and there are many terminal emulation programs that can be selected, such as xterm, rxvt, or zterm. All of these use the method of simulation to simulate a real terminal device.

The actual terminal equipment is different from the display connected to the host because they have different hardware facilities. An actual character terminal is usually connected to the serial port of the host through a long cable, and the display of the host is connected to the display card of the host. For the display of the host computer, the video image is saved in the display memory on the display card of the host computer, and the image of the display screen of the terminal device is saved on the display card with simple functions of the terminal device .

In order to control the format, position and other attributes of the information displayed on the screen on the terminal, the host needs to send a Control Code and / or Escape Sequence to the terminal.

The control code (or Control Character) refers to the first 32 characters in the ASCII code table. These control characters include: Carriage-Return, Line-Feed, Backspace, Escape-Character, Tab, and Bell (Bell). These control characters themselves are usually not displayed on the terminal screen.

Since there are too few control characters, it is far from enough to control various attributes of the terminal, so a method of using escape sequences to control the attributes of the terminal is invented. The escape sequence consists of an escape character (Escape – ESC) followed by a sequence of ordinary characters. When the terminal receives an escape character, it will treat the following characters as a command sent by the host and interpret the character sequence. After recognizing the end of a valid escape sequence, the terminal executes the control commands of the host. Subsequent received characters will still be displayed on the screen (unless they are also control characters or escape character sequences).

ASC II stopwatch

The display characters we input are all ASC II standards for the current system. It is necessary to understand this. For some special instructions, it is more convenient for ASC II to use escape characters.

​ ------------
​ Decimal Octal Hex Binary Value
​ 10 08 16 02


​ 000 000 000 00000000 NUL (Null char.)
​ 001 001 001 00000001 SOH (Start of Header)
​ 002 002 002 00000010 STX (Start of Text)
​ 003 003 003 00000011 ETX (End of Text)
​ 004 004 004 00000100 EOT (End of Transmission)
​ 005 005 005 00000101 ENQ (Enquiry)
​ 006 006 006 00000110 ACK (Acknowledgment)
​ 007 007 007 00000111 BEL (Bell)
​ 008 010 008 00001000 BS (Backspace)
​ 009 011 009 00001001 HT (Horizontal Tab)
​ 010 012 00A 00001010 LF (Line Feed)
​ 011 013 00B 00001011 VT (Vertical Tab)
​ 012 014 00C 00001100 FF (Form Feed)
​ 013 015 00D 00001101 CR (Carriage Return)
​ 014 016 00E 00001110 SO (Shift Out)SHIFT
​ 015 017 00F 00001111 SI (Shift In)
​ 016 020 010 00010000 DLE (Data Link Escape)
​ 017 021 011 00010001 DC1 (XON) (Device Control 1)
​ 018 022 012 00010010 DC2 (Device Control 2)
​ 019 023 013 00010011 DC3 (XOFF)(Device Control 3)
​ 020 024 014 00010100 DC4 (Device Control 4)
​ 021 025 015 00010101 NAK (Negative Acknowledgement)
​ 022 026 016 00010110 SYN (Synchronous Idle)
​ 023 027 017 00010111 ETB (End of Trans. Block)
​ 024 030 018 00011000 CAN (Cancel)
​ 025 031 019 00011001 EM (End of Medium)
​ 026 032 01A 00011010 SUB (Substitute)
​ 027 033 01B 00011011 ESC (Escape)
​ 028 034 01C 00011100 FS (File Separator)
​ 029 035 01D 00011101 GS (Group Separator)
​ 030 036 01E 00011110 RS (Request to Send/Record Separator)
​ 031 037 01F 00011111 US (Unit Separator)
​ 032 040 020 00100000 SP (Space)空格
​ 033 041 021 00100001 ! (exclamation mark)
​ 034 042 022 00100010 " (double quote)
​ 035 043 023 00100011 # (number sign)
​ 036 044 024 00100100 $ (dollar sign)
​ 037 045 025 00100101 % (percent)
​ 038 046 026 00100110 & (ampersand)
​ 039 047 027 00100111 ' (single quote)
​ 040 050 028 00101000 ( (left/opening parenthesis)
​ 041 051 029 00101001 ) (right/closing parenthesis)
​ 042 052 02A 00101010 * (asterisk)
​ 043 053 02B 00101011 + (plus)
​ 044 054 02C 00101100 , (comma)
​ 045 055 02D 00101101 - (minus or dash)
​ 046 056 02E 00101110 . (dot)
​ 047 057 02F 00101111 / (forward slash)
​ 048 060 030 00110000 0
​ 049 061 031 00110001 1
​ 050 062 032 00110010 2
​ 051 063 033 00110011 3
​ 052 064 034 00110100 4
​ 053 065 035 00110101 5
​ 054 066 036 00110110 6
​ 055 067 037 00110111 7
​ 056 070 038 00111000 8
​ 057 071 039 00111001 9
​ 058 072 03A 00111010 : (colon)
​ 059 073 03B 00111011 ; (semi-colon)
​ 060 074 03C 00111100 < (less than)
​ 061 075 03D 00111101 = (equal sign)
​ 062 076 03E 00111110 > (greater than)
​ 063 077 03F 00111111 ? (question mark)
​ 064 100 040 01000000 @ (AT symbol)
​ 065 101 041 01000001 A
​ 066 102 042 01000010 B
​ 067 103 043 01000011 C
​ 068 104 044 01000100 D
​ 069 105 045 01000101 E
​ 070 106 046 01000110 F
​ 071 107 047 01000111 G
​ 072 110 048 01001000 H
​ 073 111 049 01001001 I
​ 074 112 04A 01001010 J
​ 075 113 04B 01001011 K
​ 076 114 04C 01001100 L
​ 077 115 04D 01001101 M
​ 078 116 04E 01001110 N
​ 079 117 04F 01001111 O
​ 080 120 050 01010000 P
​ 081 121 051 01010001 Q
​ 082 122 052 01010010 R
​ 083 123 053 01010011 S
​ 084 124 054 01010100 T
​ 085 125 055 01010101 U
​ 086 126 056 01010110 V
​ 087 127 057 01010111 W
​ 088 130 058 01011000 X
​ 089 131 059 01011001 Y
​ 090 132 05A 01011010 Z
​ 091 133 05B 01011011 [ (left/opening bracket)
​ 092 134 05C 01011100 (back slash)
​ 093 135 05D 01011101 ] (right/closing bracket)
​ 094 136 05E 01011110 ^ (caret/cirumflex)
​ 095 137 05F 01011111 _ (underscore)
​ 096 140 060 01100000 `
​ 097 141 061 01100001 a
​ 098 142 062 01100010 b
​ 099 143 063 01100011 c
​ 100 144 064 01100100 d
​ 101 145 065 01100101 e
​ 102 146 066 01100110 f
​ 103 147 067 01100111 g
​ 104 150 068 01101000 h
​ 105 151 069 01101001 i
​ 106 152 06A 01101010 j
​ 107 153 06B 01101011 k
​ 108 154 06C 01101100 l
​ 109 155 06D 01101101 m
​ 110 156 06E 01101110 n
​ 111 157 06F 01101111 o
​ 112 160 070 01110000 p
​ 113 161 071 01110001 q
​ 114 162 072 01110010 r
​ 115 163 073 01110011 s
​ 116 164 074 01110100 t
​ 117 165 075 01110101 u
​ 118 166 076 01110110 v
​ 119 167 077 01110111 w
​ 120 170 078 01111000 x
​ 121 171 079 01111001 y
​ 122 172 07A 01111010 z
​ 123 173 07B 01111011 { (left/opening brace)
​ 124 174 07C 01111100 | (vertical bar)
​ 125 175 07D 01111101 } (right/closing brace)
​ 126 176 07E 01111110 ~ (tilde)
​ 127 177 07F 01111111 DEL (delete)


Detailed explanation of the first 32 control characters:

Seq Decimal Sixteen abbreviation Character name
00 0x00 NO Null (empty)
^A 01 0x01 SOH Start of Heading
^B 02 0x02 STX Start of Text
^C 03 0x03 ETX End of Text (End of Text)
^D 04 0x04 ROT End of Transmission
^E 05 0x05 ENQ Enquiry (Enquiry)
^F 06 0x06 ACK Acknowledge
^G 07 0x07 BEL Bell
^H 08 0x08 BS Backspace
^I 09 0x09 HT Horizontal Tab
^J 10 0x0A LF Line Feed (line feed)
^K 11 0x0B VT Vertical Tab
^L 12 0x0C FF Form Feed (Form feed)
^M 13 0x0D CR Carriage Return
^N 14 0x0E SO Shift Out
^ O 15 0x0F AND Shift In
^P 16 0x10 ACCORDING TO Data Link Escape (Data Link Escape)
^Q 17 0x11 DC1 Device Control 1 (Device Control 1)
^R 18 0x12 DC2 Device Control 2 (Device Control 2)
^S 19 0x13 DC3 Device Control 3
^T 20 0x14 DC4 Device Control 4
^ U 21 0x15 NAK Negative Acknowledge
^ V 22 0x16 SYN Synchronous Idle
^W 23 0x17 ETB End of Transmission Block
^X 24 0x18 CAN Cancel
^ Y 25 0x19 IN End of Medium
^ Z 26 0x1A SUB Substitute (replacement)
^[ 27 0x1B ESC Escape (escape)
^\ 28 0x1C FS File Separator
^] 29 0x1D GS Group Separator
^^ 30 0x1E RS Record Separator
^_ 31 0x1F US Unit Separator
127 0x7F OF THE Delete

ANSI escape sequence

The escape sequences have different lengths. All sequences begin with the ASCII character ESC (27 / hex 0x1B), and the second byte is @A–Z[\]^_a character in the range of 0x40–0x5F (ASCII ).

The standard specifies that in an 8-bit environment, the sequence of these two bytes can be combined into a single byte in the range of 0x80-0x9F. However, on modern devices, these codes are usually used for other purposes, such as a part of UTF-8 or CP-1252 characters, so this combination is not used.

C0 codes other than ESC (usually BEL, BS, CR, LF, FF, TAB, VT, SO, and SI) may also produce effects similar to or the same as some control sequences when output.

Pressing special keys on the keyboard and outputting xterm CSI, DCS, or OSC sequences are often used to generate CSI, DCS, or OSC sequences sent from the terminal to the computer, just as the user enters using the keyboard.

CSI sequence

The CSI sequence is composed ESC [of several (including 0) "parameter bytes", several "middle bytes", and one "final byte". The character range of each part is as follows:

component Character range ASCII
Parameter byte 0x30–0x3F 0–9:;<=>?
Middle byte 0x20–0x2F Space,!"#$%&'()*+,-./
Final byte 0x40–0x7E @A–Z[\]^_a–z{

所有常见的序列都只是把参数用作一系列分号分隔的数字,如1;2;3。缺少的数字视为0(如1;;3相当于中间的数字是0,ESC[m这样没有参数的情况相当于参数为0)。某些序列(如CUU)把0视为1,以使缺少参数的情况下有意义:F.4.2。

一部分字符定义是“私有”的,以便终端制造商可以插入他们自己的序列而不与标准相冲突。包括参数字节<=>?的使用,或者最终字节0x70–0x7F(p–z{|}~)例如VT320(英语:VT320)序列CSI?25hCSI?25l的作用是打开和关闭光标的显示。

当CSI序列含有超出0x20–0x7E范围的字符时,其行为是未定义的。这些非法字符包括C0控制字符(范围0–0x1F)、DEL(0x7F),以及高位字节。

选择图形再现(SGR)参数

代码 作用 备注
0 重置/正常 关闭所有属性。
1 粗体或增加强度
2 弱化(降低强度) 未广泛支持。
3 斜体 未广泛支持。有时视为反相显示。
4 下划线
5 缓慢闪烁 低于每分钟150次。
6 快速闪烁 MS-DOS ANSI.SYS;每分钟150以上;未广泛支持。
7 反显 前景色与背景色交换。
8 隐藏 未广泛支持。
9 划除 字符清晰,但标记为删除。未广泛支持。
10 主要(默认)字体
11–19 替代字体 选择替代字体。
20 尖角体 几乎无支持。
21 关闭粗体或双下划线 关闭粗体未广泛支持;双下划线几乎无支持。
22 正常颜色或强度 不强不弱。
23 非斜体、非尖角体
24 关闭下划线 去掉单双下划线。
25 关闭闪烁
27 关闭反显
28 关闭隐藏
29 关闭划除
30–37 设置前景色 参见下面的颜色定义。
38 设置前景色 下一个参数是5;n2;r;g;b,见下。
39 默认前景色 由具体实现定义(按照标准)。
40–47 设置背景色 参见下面的颜色定义。
48 设置背景色 下一个参数是5;n2;r;g;b,见下。
49 默认背景色 由具体实现定义(按照标准)。
51 Framed
52 Encircled
53 上划线
54 Not framed or encircled
55 关闭上划线
60 表意文字下划线或右边线 几乎无支持。
61 表意文字双下划线或双右边线
62 表意文字上划线或左边线
63 表意文字双上划线或双左边线
64 表意文字着重标志
65 表意文字属性关闭 重置6064的所有效果。
90–97 设置明亮的前景色 aixterm(非标准)。
100–107 设置明亮的背景色 aixterm(非标准)。

在编程中经常看到如:\033的转义字符,使用情况具体意思如下:

转义字符 说明
\033[0m 关闭所有属性
\033[1m 设置高亮度
\033[4m 下划线
\033[5m 闪烁
\033[7m 反显
\033[8m 消隐
\033[30m-\33[37m 设置前景颜色
\033[40m-\33[47m 设置背景颜色
\033[nA 光标上移n行
\033[nB 光标下移n行
\033[nC 光标右移n行
\033[nD 光标左移n行
\033[y;xH 设置光标位置
\033[2J 清屏
\033[K 清除从光标到行尾的内容
\034[s 保存光标位置
\033[u 恢复光标位置
\033[?25l 隐藏光标
\033[?25h 显示光标

背景色:

40:黑 41:深红 42:绿 43:黄色 44:蓝色 45:紫色 46:深绿 47:白色

字体颜色:

30:黑 31:红 32:绿 33:黄 34:蓝色 35:紫色 36:深绿 37:白色

输出字体颜色:

linux终端下输出带颜色的文字只需在文字前面添加如下格式

echo -e "\033[字背景颜色;文字颜色m字符串\033[0m"`

其中\033是ESC的八进制,\033[即告诉终端后面是设置颜色的参数,显示方式,前景色,背景色均是数字

例如:
echo -e "\033[41;36m something here \033[0m"

其中41代表背景色, 36代表字的颜色

可以将所有控制参数都用上,也可以只使用前景色或背景色.

但有一点要注意,如果输出带颜色的字符后并没有恢复终端默认设置,后续的命令输出仍旧会采用之前的颜色,如果是在脚本中设置了颜色而未恢复,则整个脚本的输出都会采用之前的颜色,因此如果不希望影响后面文字的输出,最好是在输出带颜色的文字之后恢复终端默认设置,如下

如果想设置文字颜色:

echo -e "\033[30m 黑色字 \033[0m" 
echo -e "\033[31m 红色字 \033[0m" 
echo -e "\033[32m 绿色字 \033[0m" 
echo -e "\033[33m 黄色字 \033[0m" 
echo -e "\033[34m 蓝色字 \033[0m" 
echo -e "\033[35m 紫色字 \033[0m" 
echo -e "\033[36m 天蓝字 \033[0m" 
echo -e "\033[37m 白色字 \033[0m"

如果是简单设置背景颜色:

echo -e "\033[40;37m 黑底白字 \033[0m" 
echo -e "\033[41;37m 红底白字 \033[0m" 
echo -e "\033[42;37m 绿底白字 \033[0m" 
echo -e "\033[43;37m 黄底白字 \033[0m" 
echo -e "\033[44;37m 蓝底白字 \033[0m" 
echo -e "\033[45;37m 紫底白字 \033[0m" 
echo -e "\033[46;37m 天蓝底白字 \033[0m" 
echo -e "\033[47;30m 白底黑字 \033[0m" 

内容参考:

  1. 作者:BigDipper https://www.jianshu.com/p/a924a6d6ed22
  2. 作者: andylauren https://blog.csdn.net/andylauren/article/details/52235512?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2
  3. 作者:wqqq https://zhidao.baidu.com/question/1815137.html
  4. 作者:咻一咻 https://blog.csdn.net/qq_37187976/article/details/79265667

Guess you like

Origin www.cnblogs.com/xiaoqiangink/p/12718524.html