How to change the output color of echo in Linux
2014-06-15 12:23
676 查看
大纲
1、The Bash Shell
2、Output in Color
3、How can do it?
1、The Bash Shell Bash is the primary shell of the Linux machine, included here are some tips/tricks with the shell. Use the manual page and learn about PS1, PS2 and PROMPT_COMMAND. The tricks in here can be use in ~/.bash_profile or ~/.bashrc.
2、Output in Color Cool terminal output involves color, here is how to set and reset output colors using terminal control characters. To print in color one must first send the control sequences for the color to the terminal, then output the message and reset the terminal, to be nice. One can also create nifty looking prompts with the teniques described here.Console Color Table The below key describes all the different sequences that can be sent to change the display format. The disclaimer should be made that "this is how the test computer performed".
Echo in Color Test Script
The code below is a sample of echo in color, it will run through all the sequences and ouput some nice looking tables. This can be use to test what different colors will look like as well as show the hidious combinations.
3、How can do it?You can use these codes:
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
1、The Bash Shell
2、Output in Color
3、How can do it?
1、The Bash Shell Bash is the primary shell of the Linux machine, included here are some tips/tricks with the shell. Use the manual page and learn about PS1, PS2 and PROMPT_COMMAND. The tricks in here can be use in ~/.bash_profile or ~/.bashrc.
2、Output in Color Cool terminal output involves color, here is how to set and reset output colors using terminal control characters. To print in color one must first send the control sequences for the color to the terminal, then output the message and reset the terminal, to be nice. One can also create nifty looking prompts with the teniques described here.Console Color Table The below key describes all the different sequences that can be sent to change the display format. The disclaimer should be made that "this is how the test computer performed".
Style Foreground Background 1st Digit 2nd Digit 3rd Digit 0 - Reset 30 - Black 40 - Black 1 - FG Bright 31 - Red 41 - Red 2 - Unknown 32 - Green 42 - Green 3 - Unknown 33 - Yellow 43 - Yellow 4 - Underline 34 - Blue 44 - Blue 5 - BG Bright 35 - Magenta 45 - Magenta 6 - Unknown 36 - Cyan 46 - Cyan 7 - Reverse 37 - White 47 - WhiteTo instruct
echoto interpret these codes you must tell it
-en. The
-eswitch tells echo to interpret your escape sequences and
-ntells echo not to 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 the letter m. The sequence to get plain red on black would look like this:
echo -en "\033[0;31;40m"or once could say
echo -en "\033[0;31m"to only affect the foreground. Portions of the sequence can be left out but the digits are still interpreted in the same order. One can switch only the background by saying
echo -en "\033[7;43m", this would change the background to yellow without affecting the current foreground settings. Once output is complete the terminal should be reset with
echo -e "\033[0m".
Echo in Color Test Script
The code below is a sample of echo in color, it will run through all the sequences and ouput some nice looking tables. This can be use to test what different colors will look like as well as show the hidious combinations.
#/bin/bash # Show all the colors of the rainbow, should be run under bash for STYLE in 0 1 2 3 4 5 6 7; do for 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 echo done # Reset echo -e "\033[0m"
3、How can do it?You can use these codes:
Black 0;30 Dark Gray 1;30 Blue 0;34 Light Blue 1;34 Green 0;32 Light Green 1;32 Cyan 0;36 Light Cyan 1;36 Red 0;31 Light Red 1;31 Purple 0;35 Light Purple 1;35 Brown/Orange 0;33 Yellow 1;33 Light Gray 0;37 White 1;37And then use them like this in your script:
red='\e[0;31m' NC='\e[0m' # No Color echo -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
相关文章推荐
- How to change the Text color of Menu item in Android?
- How to change the text color of the selected property sheet tab in Visual C++
- HOW TO: Change the Owner of a User-Defined Data Type That Is in Use in SQL Server 2000
- How to Change the Hostname of a Linux System
- How to Output a List of Files to a File and Sort Them in Linux
- how can i change the back color on mouse out to the color specified in gridview
- How to change the background color of form using C++ - 如何用C++改变窗体的颜色
- In Oracle 11g, how to change the order of the results of a sql without “order by”?(转)
- How to use "man" effectively in the development of Linux
- how to change the default mode of a linux device
- 如何调整Dreamhost主机PHP上传尺寸的限制/How to change the maximal size of uploading in your Dreamhost
- How to change the color of a CEikLabel
- the diary of how to backup one more files in linux
- How to change the language of oracle sqlplus from chinese to english in windows(如何将oracle sql plus中的中文变成英文版)
- How to change the resolution in text mode for Linux5+
- How to Change the Hostname of a Linux System
- how to change the background color of actionbar
- How to Change the Size of a Box-Plot Label in MATLAB
- How to check the usage of net ports in linux?
- How to count the number of threads in a process on Linux