Shell terminal character color settings
When writing a program, the terminal usually needs to display various colors such as red or green on the output, so that users can be notified more clearly. The following is how to set the terminal color of the Shell system:
The Character Color of the terminal is controlled by escape sequence. It is a system display function in text mode and has nothing to do with the specific language,shell
,python
,perl
,C
,C++
Can be called.
The escape sequence isESC
Can start\033
Complete the same job (The ASCII code of ESC is expressed in decimal format as 27, which is equivalent to 33 in octal format.
).
- Display format: \ 033 [display mode; foreground color; background color; Action m;
- The display mode, foreground color, or background color have no sequential relationship, and none of them is required.
- Default color: \ 033 [0 m
The color of the text terminal can be generated using an ANSI unconventional character sequence. Example:
echo -e “\033[44;37;5m ME \033[0m COOL”
The above command sets the background to blue, foreground white, blinking cursor, output character "ME", then reset the screen to the default settings, output characters“COOL”
.“e”
Yesecho
Is used to activate the parser for special characters.“\033”
Guides unconventional character sequences.“m”
This means to set the attribute and end the sequence of unconventional characters. The valid characters in this example are:“44;37;5”
And“0”
.
Modify“44;37;5”
A combination of different colors can be generated. The values are irrelevant to the order of encoding. The optional encoding is as follows:
Encoding color/Action
- 0. Reset the property to the default setting.
- 1. Set bold
- 2. Set half brightness (simulate color display)
- 4. Set the underline to simulate the color of the color display)
- 5. Set flashing.
- 7. Set reverse Images
- 22. Set the general density
- 24. Close the underline
- 25 turn off flashing
- 27 disable reverse image
- 30 set the black foreground
- 31 set the red foreground
- 32. Set the green foreground
- 33 set the brown foreground
- 34 set the blue foreground
- 35 set purple foreground
- 36 set the blue foreground
- 37 set the white foreground
- 38. Set the underline on the default foreground color
- 39. Disable the underline on the default foreground color
- 40 set a black background
- 41 set the red background
- 42 set a green background
- 43 set brown background
- 44 set the blue background
- 45 set a purple background
- 46 set the blue background
- 47 set the white background
- 49 set the default black background
Other interesting code:
- \ 033 [2J clear screen
- \ 033 [close all keyboard lights with 0q
- \ 033 [1 Q: Set the "Scroll Lock" indicator (Scroll Lock)
- \ 033 [set the "numerical Lock" indicator (Num Lock) for 2q)
- \ 033 [set "Caps Lock" for 3q)
- \ 033 [15: 40 H move close to 15th rows, 40 Columns
- \ 007 beep
This article permanently updates the link address: