When writing a shell script, changing the display color of the shell not only changes the user experience of the shell terminal, but also more practical is to distinguish normal output by changing the color of the display content, warning, error, and other output content at different levels of attention.
1. Escape sequence)
An escape sequence is a series of characters used to change the state of computers and their attached peripheral devices. these are also known as control sequences, reflecting their use in device control. some control sequences are special characters that always have the same meaning. escape sequences use an escape character to change the meaning of the characters which follow it, meaning that the characters can be interpreted as a command to be executed rather than as data.
Simply put, escape sequences can change the state of computers and peripherals. Here, we use escape characters to change the display color of the monitor.
2. echo command
The echo command has the-e option. With this option, you can enable the explanation of the backslash escape in this command, append an appropriate escape sequence to the options below to change the display color of the echo command output. Color-related parameters of text display in echo mainly include three styles, Frontground and Background. Each parameter has seven values in the following format:
Style Foreground Background1st Digit 2nd Digit 3rd Digit0-Reset 30-Black 40-Black1-FG Bright 31-Red 41-Red2-Unknown 32-Green 42-Green3-Unknown 33-Yellow 43- yellow4-Underline 34-Blue 44-Blue5-BG Bright 35-Magenta 45-Magenta (Magenta) 6-Unknown 36-Cyan 46-Cyan (blue-green) 7-Reverse 37-White 47-White
Change Style, Foreground, and Background in echo-e "\ e [Style; Foreground; BackgroundmTHINGSTOPRINT" to the preceding number to change the display color of THINGSTOPRINT. For example, $ echo-e "\ e [1; 32; 43 mHello World! "Running result 1.
Figure 1
Note: The reason for this is Hello World! The last exclamation mark is followed by a space because the exclamation mark in unix shell is a reserved character and used to call historical commands. For example! Cmd executes the command starting with cmd that has been executed, and! Cmd: p only calls the command starting with the previous cmd and does not execute it.
We can find that the color of the shell display in all subsequent rows will be changed, which is often not what we want, so in general, we will set the display to the Reset at the end of this command. At the same time, in order not to be affected by the last command, we will also set the style value to start setting the previous reset. (Of course, if you want to set the style to underline or FGorBG Bright, you won't be able to reset it before you start setting. However, the final reset is necessary no matter what, unless you want to keep the current settings for all the subsequent displays .) The running result of the following commands is as follows: 2.
Command:
echo -e "\e[1;32;43mHello World! \e[0m" echo -e "\e[0;32;43mHello World! \e[0m" echo -e "\e[4;32;43mHello World! \e[0m" echo -e "\e[5;32;43mHello World! \e[0m"
Figure 2 running result
3. Print all the display scripts.
Script test. sh Content: #/bin/bashfor TYLE in 0 1 2 3 4 5 6 7; do for FG in 30 31 32 33 34 35 36 37; do for BG in 40 41 42 43 44 45 46 47; do CTRL = "\ 033 [$ {STYLE };$ {FG }; $ {BG} m "echo-en" $ {CTRL} "echo-n" $ {STYLE };$ {FG }; $ {BG} "echo-en" \ 033 [0 m "done echo done echodone # Resetecho-e" \ 033 [0 m"
Note: here, the echo-n option is used in the script. Its function is to control the append line break after the output content, only one echo command is used to output an empty line to achieve the goal of line feed.
Sh test. sh run the script and you can see the rainbow result. 3 (the result is a little long and only part of it is intercepted here ).
Figure 3 rainbow