In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to understand the Shell output color and control, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
Everyone knows that when you use the ls command to list files, different file types are displayed in different colors. So how to achieve such a colored text output? The answer is not complicated, whether in shell or C.
1. The implementation method under shell
Let's first talk about how to implement it under shell. This can be done with the echo command, as shown in the following example:
Echo-e "33 [32mHello, world!"
When you type this command in the terminal, do you find that the system outputs "Hello,world!" in green? not only that, but even the subsequent command prompt turns green? Don't worry, listen to me go on. The echo command-e option activates the terminal's interpretation of the backslash escape character (that is,). The 33 inside the quotation marks is used to guide the unconventional character sequence, and the function here is to guide the setting of the output attributes, followed by [32m, which sets the foreground color to green, the letter m represents the set attribute category, and the number represents the attribute value.
Settings can be used separately, for example:
Echo-e "33 [0m"
The purpose of this command is to restore the property to the default value, that is, the 0m setting item is used to restore the default value. Is everything all right with your terminal now?
After understanding this, the rest is simple. With this command, you can set a number of properties in addition to setting the text foreground color.
Other settings are listed below:
33 [0m turn off all attributes 33 [1m set high brightness 33 [4m underline 33 [5m flicker 33 [7m reverse 33 [8m blanking 33 [30m to 33 [37m set foreground color 33 [40m to 33 [47m set background color 33 [nA cursor up line 33 [nB cursor move down line 33] [nC cursor move right line 33 [nD cursor move left line 33 [y] XH sets cursor position 33 [2J clear screen 33 [K] clears content from cursor to end of line 33 [s saves cursor position 33 [u restores cursor position 33 [? 25l] hides cursor 33 [? 25h display cursor
The colors represented by the numbers are as follows:
Word background color range: 40 Muhami Muhami 49
40: black
41: crimson
42: green
43: yellow
44: blue
45: purple
46: dark green
47: White
Word color: 30-39
30: black
31: red
32: green
33: yellow
34: blue
35: purple
36: dark green
37: White
In addition, multiple settings of the same kind can be grouped together, separated by a semicolon (;).
As follows:
Echo-e "33 [20th 1H33 [1x 4x 32m HelloJournal 33 [0m"
This command begins with 33 [20 × 1H] moving the cursor to the first column of the terminal line 20, followed by 33 [1 [1 × 4X] 32m to set the text property to highlight, underline and green, and then output Hello,world; finally 33 [0m to restore the terminal property to the default value, so that you will not see that the command prompt has changed even after the command is completed.
The complex control of the output of the terminal can be realized through the combination of the above commands.
Second, how to implement it in C programming?
Having understood the above implementation method in Shell, it is very simple about how to implement it in C. It can be said that you only need to use the printf function instead of echo-e above to OK. See the following example:
Int color = 32; printf ("33 [20 [1H33] [1 / 4 [dmHello, world.33 [0m", color)
This example is similar to the last example in shell above, except that the color value is specified by the variable color (of course, it can also be specified directly).
Third, Association
When you look at this, you may wonder, is it possible to control terminal output in a similar way in other programming languages? The answer is yes! For example, in python, you can output as follows:
Color=32 print "33 [20th 1H33 [1th 4th% dhello, world.33 [0m"% color
The effect of this example is the same as that of C above.
The above content is how to understand the Shell output color and control, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.