When writing a shell script. By changing the shell's display color, you can not only change the experience of using the shell terminal, but also more useful, you can distinguish the normal output by changing the color of the display content. Output from different levels of attention, such as warning and error.
1. Escape sequence (escape sequence)
An escape sequence are a series of characters used to change the state of computers and their attached peripheral devices. These is also known as control sequences, reflecting their use in device control. Some control sequences is special characters that always has the same meaning. Escape sequences use a escape character to change the meaning of the characters which follow it, meaning that the charact ERS can be interpreted as a command-to-be executed rather than as data.
To put it simply. Is that the escape sequence can change the state of the computer and peripherals. This is used to change the display color of the display using the escape character.
2. Echo command
The-e option is available in the echo command. With this option. The ability to open an explanation of backslash escaping in this command. Appending the appropriate escape sequence to the following options can change the display color of the echo command output. echo Displays color-related references in the text with three style,frontground and background. Each of the parameters has 7 values in a detailed format such as the following:
Style Foreground background1st Digit 2nd Digit 3rd digit0-reset 30-black 40-black1-fg Brig HT 31-red 41-red2-unknown 32-green 42-green3-unknown 33-yellow 43-yellow4-unde Rline 34-blue 44-blue5-bg Bright 35-magenta 45-magenta (magenta) 6-unknown 36-cyan 4 6-cyan (blue-green) 7-reverse 37-white 47-white
By the command echo-e "\e[style; Foreground; The style, foreground, and background in the Backgroundmthingstoprint "are replaced by the numbers above to change the display color of the thingstoprint behind. Example. $ ECHO-E "\e[1;32;43mhello world!" Results of implementation 1.
Figure 1
Note: The reason for this is that the last exclamation point of Hello world! is followed by a space, because the exclamation mark in the Unix shell is a reserved character used to invoke the history command. For example,!cmd runs a command that starts with a run of CMD. !cmd:p is simply a command that starts with the previous CMD and shows it. And not run.
To be able to find that, maybe all of the shell display colors will be changed, which is often not what we want, so in general we will set the display to reset at the end of this command. At the same time. In order to not be affected by the last command, the value of the style is set before reset before setting. (Of course, suppose you want to set the style to underline or FGORBG bright and the like.) You won't be able to reset before you start Setup, but the final reset is necessary anyway, unless you want all of the next shows to be using the current settings. The following several commands perform the results for example with 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 Execution results
3. Script to print all display effects
Script test.sh content: #/bin/bashfor tyle in 0 1 2 3 4 5 6 7; Do-FG in-A-Z with a PNs; Do-BG in a . Do ctrl= "\033[${style};${fg};${bg}m" echo-en "${ctrl}" echo-n "${STYLE};${FG};${BG}" Echo-en "\ 033[0m " done echo done echodone#resetecho-e" \033[0m "
Note: The Echo's-n option is used in this script. Its role is to control the output of the content after the addition of a newline, and the corresponding only one echo command is simply to output a blank line. Achieve the purpose of line wrapping.
SH test.sh executes the script and can see the rainbow-like results. 3 (The result is a bit long, this is just a partial interception).
Figure 3 Rainbow
UNIX Changes Shell display color