Escape character \033 (set the font display effect of the terminal)

The following content comes from the study and arrangement of network resources. If there is any infringement, please inform and delete it.

reference blog

Command line special display effect\033 and sound\007_ASUS his brother's blog-CSDN blog

'\033' format: specify the output format_51CTO blog_wkt format

1. Description of \033 

As mentioned in those things about ASCII characters , ASCII characters can use octal or hexadecimal digits preceded by a backslash to represent a single character.

The \033 here means this. The 033 behind the backslash is octal, which is 27 in decimal, corresponding to the character with the decimal number 27 on the ASCII character set table, that is, ESC. This character is used to control the output format of the printer.

2. Set the display effect 

1. Format description 

In linux, we can use this character to set the font display effect on the terminal.

For example, the format of using the echo command and using \033 to control the font display effect is as follows:

echo -e " \033[parameter 1;parameter 2;parameter 3 m content to be displayed \033[0m

(1) \033[parameter 1;parameter 2;parameter 3m is the writing method of the control code, indicating the format of the content to be displayed later.

(2) Parameter 1 indicates the font background color, and the optional values ​​are 40~49, 100~107; parameter 2 indicates the font color, and the optional values ​​are 30~39, 90~97; parameter 3 indicates the display effect, and the optional values ​​are 0~8. See the appendix for the meaning of these numbers.

(3) There can be references to variables in the content to be displayed, and the contents of the variables will be expanded during display.

(4) \033[0m is a specific control code, indicating that all attributes are closed. If this control code is not added at the end of the format to close all attributes, then the format set by "\033[font background color; font color m" will be applied to all subsequent displays. Of course, this specific control code can also be modified as needed, but generally not.

2. Matters needing attention

(1) If the font background color is not set, there is no need to add a semicolon before the font color. Based on the range of the number, it can be automatically deduced whether the number is used to represent the font background color, or to represent the font color, or other settings, because different project settings use different ranges of numbers.

For example, the display effect of echo -e " \033[36m what's your name: \033[0m " is as follows:

(2) The content between the letter m and the ending \033[0m is the content to be displayed on the terminal. If the letter m is followed by a space, it will also be output as a character.

(3) If the content to be displayed ends with a special symbol (for example!), an error may be reported, and a space needs to be entered between this special symbol and \033[0m.

appendix

(1) Instructions for font color setting (numbers before and after the slash represent the same color, but the numbers behind the slash make the color brighter).

number describe
30\90 black

31\91

red
32\92 green
33\93 yellow
34\94 blue
35\95 Purple
36\96 dark green
37\97 White

(2) Instructions for font background color setting (the numbers before and after the slash represent the same color, but the numbers behind the slash make the color brighter)

number describe
40\100 black

41\101

red
42\102 green
43\103 yellow
44\104 blue
45\105 Purple
46\106 dark green
47\107 White

(3) Control code description

control code meaning
\033[0m close all properties 
\033 [1 m set height width
\033[4m underline 
\033 [5 m flashing 
\033[7m reverse display
\033[8m Blanking
\033[30m ~ \033[37m   set foreground color
\033[40m ~ \033[47m set background color
\033[nA   Move the cursor up n lines
\033[nB Move the cursor down n lines 
\033[nC Move the cursor to the right n lines 
\033[nD Move the cursor to the left n lines 
\033[y;xH set cursor position 
\033[2J clear screen 
\033[K Clear from cursor to end of line 
\033[s save cursor position
\033[u restore cursor position
\033[?25l hide cursor
\033[?25h show cursor

Guess you like

Origin blog.csdn.net/oqqHuTu12345678/article/details/129327297