大纲
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 - White
To instruct echo
to interpret these codes you must tell it -en
. The -e
switch tells echo to interpret your escape sequences and -n
tells echo not to make a newline at the end. The activation sequence is \033 or \e
this 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;37
And 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
本文出自 “Share your knowledge” 博客,请务必保留此出处http://skypegnu1.blog.51cto.com/8991766/1426548
How to change the output color of echo in Linux,布布扣,bubuko.com
How to change the output color of echo in Linux
原文地址:http://skypegnu1.blog.51cto.com/8991766/1426548