How to change the output color of echo in Linux

Source: Internet
Author: User

Outline

1. The Bash Shell

2. Output in Color

3. How can I do it?


1. The Bash Shell

Bash is the primary shell of the "Linux Machine", included here's some tips/tricks with the shell. Use the manual page and learn about PS1, PS2 and Prompt_command. The tricks in this can be use in ~/.bash_profile or ~/.BASHRC.


2. Output in Color

Cool terminal output involves color, here are how to set and reset output colors using Terminal Control Charac Ters. To print in color one must first send the control sequences for the color to the terminal, then output the message and res ET the terminal, to is nice. One can also create nifty looking prompts with the teniques described here.

Console Color Table

The below key describes all the different sequences the can is sent to change the display format. The disclaimer should was made that "this is what the test computer performed".

style           foreground       Background1st Digit       2nd Digit        3rd digit0 - reset       30 -  Black      40 - Black1 - FG Bright    31 - Red        41 - Red2 -  unknown     32 - green      42 -  green3 - unknown     33 - yellow     43  - Yellow4 - Underline   34 - Blue        44 - Blue5 - BG Bright   35 - Magenta     45 - magenta6 - unknown     36 - cyan        46 - Cyan7 - Reverse     37 - White       47 - white

To instructechoTo interpret these codes your must tell it-en. The-eSwitch tells Echo to interpret your escape sequences and-nTells Echo not-make a newline at the end. The activation sequence is\033 or \eThis starts the output modification codes. Next is a[followed by a digits from the above list to represent the style, foreground and background. The sequence is terminated by them. The sequence to get plain red in black would look like this:echo -en "\033[0;31;40m"Or once could sayecho -en "\033[0;31m"To only affect the foreground. Portions of the sequence can is left out but the digits is still interpreted in the same order. One can switch only the background by sayingecho -en "\033[7;43m", this would change the background to yellow without affecting the current foreground settings. Once output is complete the terminal should was reset withecho -e "\033[0m".

Echo in Color Test Script

The code below is a sample of echo in color, it'll run through all the sequences and ouput some nice looking tables. This can is use to test what different colors would look like as well as show the hidious combinations.

#/bin/bash# Show All the colors of the rainbow, should is run under Bashfor STYLE in 0 1 2 3 4 5 6 7; Do-FG in 30 31 32 33 34 35 36 37; Do-for-BG in 40 41 42 43 44 45 46 47; Do ctrl= "\033[${style};${fg};${bg}m" Echo-en "${ctrl}" Echo-n "${STYLE};${FG};${BG}" Echo-en "\033[ 0m "done echo done echodone# resetecho-e" \033[0m "


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/2E/E0/wKiom1OdG92Dbh4IAAM_q7ChBH8366.jpg "title=" 1.png " alt= "Wkiom1odg92dbh4iaam_q7chbh8366.jpg"/>


3. How can I do it?

You can use these codes:

black        0;30     dark gray      1;30Blue         0;34      Light Blue    1;34Green        0; 32     light green   1;32cyan          0;36     Light Cyan    1;36Red           0;31     Light Red      1;31purple       0;35     light  Purple  1;35Brown/Orange 0;33     Yellow         1;33Light Gray   0;37     White     &nbsP;    1;37 

And then use the them like this in your script:

Red= ' \e[0;31m ' nc= ' \e[0m ' # No colorecho-e "${red}hello stackoverflow${nc}"

Q:DOESN ' t work for me-output: \e[0;31mhello world\e[m

A:did you try it with "-e"? It tells Echo to enable backslash escapes.


Https://wiki.archlinux.org/index.php/Color_Bash_Prompt

Http://blog.chinaunix.net/uid-1835840-id-2831465.html

Http://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux

Http://www.cnblogs.com/lr-ting/archive/2013/02/28/2936792.html



This article is from the "Share Your Knowledge" blog, so be sure to keep this source http://skypegnu1.blog.51cto.com/8991766/1426548

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.