In-depth understanding of Shell output color and control

高洛峰
Release: 2023-03-05 14:58:01
Original
1395 people have browsed it

Preface

Everyone knows that when using the ls command to list files, different file types will be displayed in different colors. So how to achieve such colored text output? The answer is not complicated, whether using shell or C language.

1. Implementation method under shell

Let’s first talk about how to implement it under shell. This can be achieved with the echo command. See the following example:

echo -e "33[32mHello, world!"
Copy after login

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 turned green? Don't worry, just listen to me. The -e option of the echo command activates the terminal's interpretation of the backslash escape character (ie). 33 in quotation marks is used to guide unconventional character sequences. The function here is to guide the setting of output attributes. The following [32m is to set the foreground color to green. The letter m represents the set attribute category, and the number represents the attribute value.

Settings can be used individually, for example:

echo -e "33[0m"
Copy after login

The function of this line of command is to restore the properties to the default values, which means that the 0m setting item is used to restore the default values. Is everything working properly on your terminal now?

After understanding these, the rest is easy. With this command, in addition to setting the text foreground color, you can also set many properties.

Other setting items are listed below:

33[0m 关闭所有属性
33[1m 设置高亮度
33[4m 下划线
33[5m 闪烁
33[7m 反显
33[8m 消隐
33[30m 至 33[37m 设置前景色
33[40m 至 33[47m 设置背景色
33[nA 光标上移n行
33[nB 光标下移n行
33[nC 光标右移n行
33[nD 光标左移n行
33[y;xH设置光标位置
33[2J 清屏
33[K 清除从光标到行尾的内容
33[s 保存光标位置
33[u 恢复光标位置
33[?25l 隐藏光标
33[?25h 显示光标
Copy after login

The colors represented by each number are as follows:

Character background color Range: 40----49

40:Black

41:Crimson

42:Green

43: Yellow

44: Blue

45: Purple

46: Dark green

47: White

Color: 30-----------39

30: Black

31:Red

32:Green

33:Yellow

34:Blue

35:Purple

36: Dark green

37: White

In addition, multiple setting items of the same type can be combined together with a semicolon (;) in the middle separated.

is as follows:

echo -e "33[20;1H33[1;4;32mHello,world33[0m"
Copy after login

This line of command first moves the cursor to line 20, column 1 of the terminal with 33[20;1H, and then 33[1;4; 32m sets the text properties to highlighted, underlined and green, and then outputs Hello, world; finally 33[0m returns the terminal properties to their default values, so that you will not see the command prompt change after the command is completed. That's it.

Complex control of terminal output can be achieved through the combination of the above commands.

2. How to implement it in C programming?

After understanding the above implementation method in Shell, it is very simple to implement it in C. It can be said that you only need to use the printf function to replace the echo -e above. See the following example:

int color = 32;
 
printf("33[20;1H33[1;4;%dmHello, world.33[0m", color);
Copy after login

This example is similar to the last example in the shell above, except that the color value here is passed through the variable color to specify (of course, you can also specify it directly).

3. Lenovo

After seeing this, you may be wondering, can similar methods be used to control terminal output in other programming languages? The answer is yes! For example, in python, the output can be as follows:

color=32
 
print “33[20;1H33[1;4;%dHello, world.33[0m"%color
Copy after login

The effect of this example is the same as the C example above.

Summary

The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate. .

For more in-depth understanding of Shell output color and control related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!