Deep understanding of shell output color and control _linux shell

Source: Internet
Author: User
Tags echo command set background in python

Objective

We all know that when you use the LS command to list file lists, different file types are displayed in different colors. So how do you implement this color-colored text output? The answer is not complicated, whether it's in the shell or the C language.

First, the implementation of the shell under the method

First, under the shell, how to achieve. This can be achieved with the echo command, as in the following example:

  Echo-e "33[32mhello, world!"

When you hit this command in the terminal, did you find out that the system was exporting "hello,world!" in green, and that even the command prompt turned green? Don't worry, listen to me. echo -e the function of the command option is to activate the interpretation of the backslash escape character (that is) for the terminal. The quotation mark 33 is used to boot an irregular sequence of characters, where the function is to boot the Set output property, the 32m is to set the foreground color to green, the letter m to set the attribute category, and the number to represent the property value.

Settings can be used separately, for example:

  Echo-e "33[0m"

The purpose of this line of command is to restore the property to the default value, which means that the 0m set item is used to restore the default value. Is your terminal all right now?

Understanding these, the rest is simple. With this command, you can set many properties in addition to setting the foreground color of the text.

The other settings are listed below:

  33[0m Turn off all properties
  33[1m set high brightness 33[4m underline 33[5m blinking 33[7m 33[8m fade 33[30m
  to 33[37m set foreground color
  33[40m to 33[47m Set Background color
  33[na cursor move n line 
  33[nb cursor down n line
  33[nc cursor right n row
  33[nd cursor left n row 33[y;xh
  set cursor position
  33[2j screen
  33[k clears the content from the cursor to the end of the line
  33[s save cursor position 
  33[u restore cursor position
  33[?25l Hide cursor
  33[?25h display cursor

The colours represented by the figures are as follows:

Word Background color range:----49

40: The Black

41: Crimson

42: Green

43: Yellow

44: Blue

45: Purple

46: Dark Green

47: White

Character Color: 39-----------

30: The Black

31: Red

32: Green

33: Yellow

34: Blue

35: Purple

36: Dark Green

37: White

In addition, many of the same types of settings can be grouped together, separated by semicolons (;).

As follows:

  Echo-e "33[20;1h33[1;4;32mhello,world33[0m"

This line of command first 33[20;1h the cursor to the terminal 20th row 1th, followed by 33[1;4;32m the Text property to a highlighted, underlined and green color, then output Hello,world, and finally 33[0m to restore the Terminal property to its default value. This will not see the command prompt after the command is complete.

Through the combination of the above various commands can achieve the terminal output of complex control.

Second, how to implement in C programming?

Understand the above implementation in the shell, about how to implement in C is very simple. It's OK to just use printf a function instead of the top echo - E. See the following example:

  int color =;

  printf ("33[20;1h33[1;4;%dmhello, world.33[0m", color);

This example is similar to the last example in the top shell, where the color value is specified by the variable color (or, of course, directly).

Third, Lenovo

See here you may think, in other programming languages can also use a similar method to achieve the control of the terminal output? The answer is YES! In Python, for example, you can output the following:

  color=32

  print "33[20;1h33[1;4;%dhello, world.33[0m"%color

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

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.