How to output colored text with Shell and C in Linux terminal

Source: Internet
Author: User
Tags echo command

We know that when using the LS command to list file lists, different file types are displayed in different colors. So how do you implement such a colored text output? The answer is not complicated, either in the shell or the C language.

First, the implementation method under the shell

First of all, under the shell, how to achieve. This can be achieved with the echo command, see the following example:

Echo-e "\033[32mhello, world!"

When you hit this command in the terminal, did you find that the system was using green to output "hello,world!", more than that, even after the command prompt has become green? Do not worry, listen to me continue to say. The echo command-e option activates the terminal's interpretation of the backslash escape character (that is, \). The quotation mark \033 is used to guide the unconventional character sequence, where the function is to boot the output property, the back of [32m is the foreground color is set to green, the letter M is the Set property category, the number represents the property value. Settings can be used alone, for example:

Echo-e "\033[0m"

The purpose of this line of command is to restore the property to the default value, which means that the 0m setting is used to restore the default value. Now, is your terminal all right again?

With this understanding, the rest is simple. With this command, you can set many properties in addition to setting the text foreground color. The other settings are listed below:

--------------------------------------------------------------------------

\033[0m Close All Properties
\033[1m Setting High brightness
\033[4m Underline
\033[5m Flashing
\033[7M Reverse Display
\033[8m blanking
\033[30m to \33[37m setting the foreground color
\033[40m to \33[47m setting the background color
\033[na the cursor to move n rows
\033[NB cursor down n rows
\033[NC cursor right shifts n rows
\033[nd cursor left n rows
\033[Y;XH Setting the cursor position
\033[2J Clear Screen
\033[k clears the contents from the cursor to the end of the line
\033[s Save Cursor position
\033[u Restore cursor Position
\033[?25l Hide Cursor
\033[?25h Display cursor

--------------------------------------------------------------------------

The colors represented by the numbers are as follows:

Word background color range: 49----
40: Black
41: Crimson
42: Green
43: Yellow
44: Blue
45: Purple
46: Dark Green
47: White

Word color: 39-----------
30: Black
31: Red
32: Green
33: Yellow
34: Blue
35: Purple
36: Dark Green
37: White

In addition, a variety of similar settings can be grouped together, separated by semicolons (;). As follows:

Echo-e "\033[20;1h\033[1;4;32mhello,world\033[0m"

This line of command first \033[20;1h the cursor to the 20th row 1th column of the terminal, after which \033[1;4;32m sets the Text property to be highlighted, underlined, and the color is green, and then outputs Hello,world, and finally \033[0m restores the terminal property to its default value. This will not see the command prompt after the command has been completed is changed.

Through the combination of the above commands can be achieved to the terminal output complex control.

Second, how to implement in C programming?

Understanding the implementation of the above in the shell, about how to implement in C is very simple. It can be said that only the printf function to replace the upper echo-e is OK. See the following example:

int color = 32;

printf ("\033[20;1h\033[1;4;%dmhello, world.\033[0m", color);

This example is similar to the last example in the shell above, except that the color value is specified by the variable color (or, of course, it can be specified directly).

Third, Lenovo

See here you may wonder, is it possible to use similar methods in other programming languages to control the output of the terminal? The answer is YES! In Python, for example, you can output the following:

Color=32

Print "\033[20;1h\033[1;4;%dhello, world.\033[0m"%color

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

How to output colored text with Shell and C in Linux terminal

Related Article

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.