Colorful output of Terminal characters
Recently, I have been improving the deployment script of the site. When debugging, there is some intermediate status information that I want to highlight, so I need to modify the output style of the Terminal characters. How to modify it? Look down.
Modify terminal character output style
Bash command | Output preview |
---|---|
echo "hello world" |
|
echo -e "\e[31mHello World\e[0m" |
|
echo -e "\e[31mHello\e[0m World" |
|
echo -e "\e[31;47mHello\e[0m World" |
As described in the table above, we change the font color (foreground color) and background color (backcolor) of the output characters. Usually, achieving this effect already meets my needs this time. How is it achieved?
Run help echo
in the terminal, we can see the brief instructions for using the echo
command, part of which is as follows:
$ help echo echo: echo [-neE] [arg ...] Write arguments to the standard output. Display the ARGs, separated by a single space character and followed by a newline, on the standard output. Options: -e enable interpretation of the following backslash escapes Allow interpretation of trailing backslash escapes -E explicitly suppress interpretation of backslash escapes Disable interpretation of following backslash escapes `echo' interprets the following backslash-escaped characters: \e escape character <Esc> ... Exit Status: Returns success unless a write error occurs.
Now we understand, -e
is ‘allow interpretation of the backslash escape’, and \e
represents
.
echo "hello world" echo -e "\e[31mHello World\e[0m" echo -e "\e[31mHello\e[0m World" echo -e "\e[31;47mHello\e[0m World"
Let’s analyze this format again and exclude the directly output characters hello world, such as \e[31m
, \e[0m
, \e[31;47m
This kind of characters are the ones that change the character output style!
So, what are they? CSI!
CSI
CSI (Control Sequence Introducer, control sequence adapter; control sequence guide code).
The CSI sequence consists of ESC [
(also written as \e[
or \033
), several (including 0) “parameter bytes”, It consists of several “intermediate bytes” and a “final byte”.
It’s not that complicated, at least you don’t need to remember it. You just need to use
\e[
to guide a CSI sequence.
All common sequences simply use the argument as a series of semicolon-separated numbers, such as 1;2;3
. Missing numbers are treated as 0
(such as 1;;3
, which is equivalent to the middle number being 0
, ESC[m
> In this case, if there is no parameter, it is equivalent to the parameter being 0
).
CSI is a type of ANSI escape sequence, a standard for escape sequences for in-band signals used to control cursor position, color, and other options on video text terminals.
ANSI Escape Sequences – Embed certain byte sequences in the text, most starting with the
Esc [
character. The terminal will interpret these byte sequences as corresponding instructions instead of ordinary character encodings.
In CSI, the parameter mode CSI n m
(such as \e31m
) is called SGR.
SGR
SGR (Select Graphic Rendition), by setting corresponding parameters, changes the character output style, such as font color, background coloring, bold, italics, etc.
Some of its commonly used parameters are as follows:
Code | Function | Remarks | Example |
---|---|---|---|
0 | Reset/Normal | Turn off all properties. | |
1 | Bold or increase intensity | ||
2 | Weakening (reducing strength) | Not widely supported. | |
3 | Italic | Not widely supported. Sometimes viewed as reverse phase display. | |
4 | Underline | ||
5 | Slow flashing | Less than 150 times per minute. | |
6 | Flashing quickly | MS-DOS ANSI. SYS; more than 150 per minute; not widely supported . | |
7 | Inverse display | The foreground color and background color are exchanged. | |
8 | Hide | Not widely supported. | |
9 | Strikeout | The characters are clear but marked for deletion. Not widely supported. | |
10 | Main (default) font | ||
21 | Turn off bold or double underline | Turn off bold not widely supported; double underline has little support. | |
22 | Normal color or intensity | Neither strong nor weak. | |
23 | Non-italic, non-cusp | ||
24 | Turn off underlining | Remove single and double underlines. | |
25 | Turn off flashing | ||
27 | Turn off reverse display | ||
28 | Turn off hiding | ||
29 | Turn off erasing | ||
30–37 | Set the foreground color | See the color table below . | |
38 | Set the foreground color | The next parameter is 5;n Or 2;r;g;b , see below. |
|
39 | The default foreground color | is implementation-defined (per the standard). | |
40–47 | Set the background color | See the color table below. | |
48 | Set background color | The next parameter is 5;n Or 2;r;g;b , see below. |
|
49 | Default background color | Implementation-defined (per standard). |
Observe carefully 1-9 and 21-29, they correspond one to one, and the latter is exactly the effect of turning off the former. Let’s focus on the foreground and background colors and see how they are distributed.
The initial specs only came in 8 colours, with only their names given. SGR parameters 30-37 select the foreground color, 40-47 select the background color.
Quite a few terminals implement “bold” (SGR code 1) as a brighter color instead of a different font, thus providing 8 additional foreground colors, but usually not for background colors, although sometimes Reverse display (SGR code 7) allows this. For example: to display black text on a white background use
ESC[30;47m
, to display red text useESC[31m
, to display bright red text useESC[1 ;31m
. To reset to default colors useESC[39;49m
(not supported by some terminals), to reset all properties useESC[0m
. Later terminals added the ability to directly specify “bright” colors using 90-97 and 100-107.
> 3/4 bit 8 colors - black, red, green, yellow, blue, magenta, cyan, white
As 256-color lookup tables became more common on graphics cards, corresponding escape sequences were added to select from the predefined 256 colors. Anyway, the principle is the same. like:
Bash command | Output preview |
---|---|
echo -e "\e[38;5;183mhello world\e[m" |
|
echo -e "\e[48;5;125mhello world\e[m" |
\e[38;5;183m
> 8-bit 256 color mode
If you don’t want to remember so many color numbers, but you happen to be familiar with RGB, then you can also use it (such as \e[38;2;R;G;Bm
) to easily set the color. like:
Bash command | Output preview |
---|---|
echo -e "\e[38;2;255;1;1mhello world\e[m" |
|
echo -e "\e[48;2;255;1;1mhello world\e[m" |
At this point, you can basically easily achieve the character effects you want to output.
reference
- Introduction & ANSI Escape Codes – IntelliJ IDEA Guide
- ANSI escape code – Wikipedia
- ANSI escape sequences – Wikipedia, the free encyclopedia
- Detailed explanation of the color and format of output in shell bash terminal (super detailed)
- shell echo display color – Zhihu