How to use Shell and C to output colored text on Linux terminal

Source: Internet
Author: User

From http://blog.csdn.net/acmee/article/details/6613060

We know that when you use the ls command to list files, different file types are displayed in different colors. How can we achieve this color-based text output? The answer is not complex, whether using shell or C language.

I. Implementation Method in shell

      First, let's talk about how to implement it in shell. You can use the echo command. See the following example:

      Echo-E"\ 033 [32 mHello, world! "

      After you typed this command in the terminal, did you find that the system outputs "Hello, world!" in green! ", More than that, after the command prompt is turned green? Don't worry. Let me continue. The echo command-e option is used to activate the terminal's explanation of the backslash escape character. \ 033 in quotation marks is used to guide the sequence of unconventional characters. Here, the function is to set the output attribute, and the [32 m behind is to set the foreground color to green, and the letter m indicates the set attribute category, number indicates the property value. The settings can be used independently, for example:

      Echo-e"\ 033 [0 m"

      This command is used to restore the property to the default value, that is, the 0 m setting item is used to restore the default value. Is everything on your terminal now normal?

      After understanding this, the rest is simple. In addition to setting the text foreground, you can also set many attributes. Other settings are listed below:

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

     \ 033 [0 m close all attributes
     \ 033 [1 m set high brightness
     \ 033 [4 m underline
     \ 033 [5 m flashing
     \ 033 [7 m reverse display
     \ 033 [8 m blanking
     \ 033 [30 mSet the foreground color to \ 33 [37 m
     \ 033 [40 mSet the background color to \ 33 [47 m
     \ 033 [move the NA cursor up n rows
      \ 033 [move the Nb cursor down n rows
     \ 033 [the NC cursor shifts n rows right
     \ 033 [Nd cursor shifts n rows left
     \ 033 [Y; XH: Set the cursor position
     \ 033 [2J clear screen
     \ 033 [K clear content from cursor to end of line
     \ 033 [s Save the cursor position
      \ 033 [u restore cursor position
     \ 033 [? 25l hide the cursor
     \ 033 [? 25 h show cursor

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

      The color of each digit is as follows:

     Color Range: 40----49
    40: Black
    41: dark red
   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 configuration items of the same type can be combined and separated by semicolons. As follows:

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

     This line of command First \ 033 [20; 1 h move the cursor to the terminal 20th row 1st column, followed by \ 033 [1; 4; 32 m set the text property to highlighted, underlined, and green, and then output Hello, world; finally \ 033 [0 m restore terminal property to the default value, in this way, the command prompt after the command is complete will not be changed.

     The combination of the above commands can achieve complex control over terminal output.

2. How to implement it in C programming?

      After understanding the above implementation methods in shell, it is very easy to implement in C. It can be said that you only need to replace the echo-E above with the printf function. See the following example:

     Int color = 32;

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

     This example is similar to the last example in the above shell, but the color value is specified by the variable color (of course, you can also specify it directly ).

Iii. Lenovo

     If you see this, you may wonder if you can use similar methods 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 "\ 033 [20; 1 h \ 033 [1; 4; % dHello, world. \ 033 [0 m" % color

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

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.