Escape sequences escape sequences and Linux echo command multiple color display

Source: Internet
Author: User
Tags echo command set background

Read a lot of Chinese blog posts, most of the article is about the use of echo color, I really do not love to die, trace back, really look at the escape sequence is what?

Escape characters, children who have studied C are aware that some control character outputs are supported in the famous printf function, such as \ t, \ n, and so on. We often call them escape characters.

What is an escape sequence?

1, Wikipedia (Translator is afraid of translation deviation, keep the original)
In computing, ANSI escape code (or escape sequences) are the method of In-band signaling to control formatting, color, and Other output options on video text terminals.
In the computer world, an ANSI escape encoding or escape sequence is a method of controlling the format, color, and other output options of a video text terminal with an in-band signal (metadata and control signals).
To encode this formatting information, it embeds certain sequences of bytes into the text, which has to be interpreted SP Ecially, not as codes of characters.
In order to encode these formatting information, it embeds a sequence of bytes into the text, which must be interpreted in a special way and should not be used as the literal meaning of the character.


Although hardware text terminals has become increasingly rare in the 21st century, the relevance of this standard persist s because most terminal emulators interpret at least some of the ANSI escape sequences in the output text.
Although hardware character terminals have become increasingly rare in the 21st century, this standard still has practical value because most terminal emulators can at least interpret some ANSI escape sequences in the output text.
One notable exception is the Win32 console component of Microsoft Windows.
But Microsoft's Win32 console component is an obvious exception.


2, Microsoft's explanation
The combination of characters that contain a backslash (\) followed by a letter or a combination of numbers is called an escape sequence. To display line breaks, single quotes, or some other character constant, you must use an escape sequence. Therefore the escape sequence is treated as a single character that is valid as a character constant.

second, escape sequence definition
Sequence elements
Sequence elements
Escape sequences start with the character ESC (ASCII decimal 27/hex 0x1b/octal 033).
Escape sequences begin with the ESC control character.
For the character sequences, the second character are in the range ASCII (@ to _).
For a 2-character sequence, the second character is 64 to 95 of ASCII. (@ To _, plus all uppercase letters and []\^)
However, most of the sequences is more than the characters, and start with the characters ESC and [(left bracket).
However, most of the sequences are more than 2 characters long and begin with the ESC control character and the left square bracket.
This sequence are called CSI for Control sequence introducer (or Control sequence initiator). The final character of these sequences is in the range ASCII from 126 (@ to ~).
A sequence is called a CSI, which is the short name that controls the sequence bootstrapper or controls the sequence initiator. The last character of this sequence is in the ASCII range 64 to 126.
There is a single-character CSI (155/0x9b/0233) as well.
There is also a single-character CSI (155/0x9b/0233).

The esc+[two-character sequence is more often used than the single-character alternative, for details see C0 and C1 contr OL codes.
esc[, these two character sequences use more than a single character, and the details refer to the C0 and C1 control codes.
Only the two-character sequence are recognized by devices, support just ASCII (7-bit bytes) or devices that support 8-b It bytes the 0x80–0x9f control character range for other purposes.
Devices that support only ASCII (7-bit bytes), or that support 8-bit bytes but use 0x80-0x9f to control the character area for other purposes only recognize sequences of 2 characters.
On terminals This use UTF-8 encoding, both forms take 2 bytes (CSI in UTF-8 are 0xC2, 0x9b) but the esc+[sequence is clear Er.
On terminals that use UTF-8 encoding, both forms use 2 bytes (CSI in UTF-8 is 0xC2, 0x9b), but the esc[sequence is clearer.

Though Some encodings use multiple bytes per character, the following discussion are restricted to ASCII characters, and so Assumes each character was directly represented by a single byte.
Although some encodings use multibyte characters, the following discussion is only for ASCII characters, so it is assumed that each character is represented by a single byte.

30–37    Set text color (foreground)     x, where x is from the color table below &NB Sp  
38    Set xterm-256 text color (foreground) [dubious–discuss]    next Arguments is 5;x where x is color index (0..255)    
39    Default text color (foreground) &N bsp;   implementation defined (according to standard)    
40–47    Set background color    + x, where x is from the color table below    
48    Set xterm-256 Background color    next arguments be 5;x where x is color index (0..255)    
49 &NBSP ;  Default background color    implementation defined (according to standard)

three, color control sequence
Colors
Text colors (and SGR parameters in general) is manipulated using CSI n1 [; N2 [; ...]] m sequences, where each N1, N2,. . is an SGR parameter as shown above.
The text color (and the SGR (Select Graphic rendition) parameter) uses CSI N1 [; N2 [; ...]] m sequence to process, as shown above, each of the sequences in N1, N2, ... is a SGR parameter.
Thus, for instance, you use codes 30+i to specify foreground color, 40+i to specify background color, where I am the Numbe R in the desired color's column header in the table below.
So, for example, you use 30+i for the foreground color, 40+i for the background color, and I are the color numbers in the following table.

Color Table
Intensity 0 1 2 3 4 5 6 7
Normal black red green yellow blue magenta cyan white
Bright black red green yellow blue magenta cyan white

The following examples can be used with the printf utility, where \x1b[implements the csi:to switch the foreground color To black, use \x1b[30m; to switch to red, use \x1b[31m; utilizing the "bold" parameter, Gray would is \x1b[30;1m; to get Bold red, use \x1b[31;1m. To the reset colors to their defaults, use \x1b[39;49m (or reset all attributes with \x1b[0m).
The following example uses the printf feature, in which \x1b[implements CSI:
Convert foreground color to black, use \x1b[30m
Convert to red, use \x1b[31m
If you use bold parameters, grey writing \x1b[30;1m
Get red bold, use \x1b[31;1m
Reset color to default value, use \x1b[39;49m (or reset all properties with \x1b[0m)

\033[0m Reset to Normal
\033[1m setting high brightness or bold
\033[4m Underline
\033[5m Flashing
\033[7M Reverse Display
\033[8m blanking
\033[30m--/33[37m setting foreground color
\033[40m--/33[47m Set background color

Common representations of the control ESC \e, \x1b (\x1b), \033 can be
\e refers to escape, corresponding to the octal \033, corresponding to the hexadecimal \x1b

iv. Examples of:
Using the echo command from CentOS 6
1, $ echo-e "\033[1;31;42mmine is red \e[0m"
Red character with Green Bottom highlighted
2, $ echo-e "\033[4mm is Red"
underlined, but not closed, underscores persist until a 3rd example is encountered
3, $ echo-e "\x1b[0m"
Turn off all properties and return to normal


Special Thanks, wife in translation of the strong support and good advice.


See literature
Http://en.wikipedia.org/wiki/ANSI_escape_code

This article is from the "End of Nanshan" blog, please be sure to keep this source http://me2xp.blog.51cto.com/6716920/1437542

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.