Linux Terminal Color
来源:互联网 发布:三国杀官方淘宝旗舰店 编辑:程序博客网 时间:2024/05/16 05:43
Bash tips:Colors and Formating(ANSI/VT100 Control sequences)
The ANSI/VT100 terminals and terminal emulators are not just able to display black and white text ; they can display colors and formatted texts thanks to escape sequences. Those sequences are composed of the Escape character (often represented by ”^[
” or ”<Esc>
”) followed by some other characters: ”<Esc>[
FormatCodem
”.
In Bash, the <Esc>
character can be obtained with the following syntaxes:
\e
\033
\x1B
Examples:
echo -e "\e[31mHello World\e[0m"
echo -e "\033[31mHello\e[0m World"
NOTE¹: The -e
option of the echo
command enable the parsing of the escape sequences.
NOTE²: The ”\e[0m
” sequence removes all attributes (formatting and colors). It can be a good idea to add it at the end of each colored text. ;)
NOTE³: The examples in this page are in Bash but the ANSI/VT100 escape sequences can be used in every programming languages.
Formating
Here are the most commonly supported control sequences for formatting text. Their support depends on the used terminal (see the compatibility list).
Set
Code Description Example Preview 1Bold/Brightecho -e "Normal \e[1mBold"2Dimecho -e "Normal \e[2mDim"4Underlinedecho -e "Normal \e[4mUnderlined"5Blink 1)echo -e "Normal \e[5mBlink"7Reverse (invert the foreground and background colors)echo -e "Normal \e[7minverted"8Hidden (usefull for passwords)echo -e "Normal \e[8mHidden"
Reset
Code Description Example Preview 0Reset all attributesecho -e "\e[0mNormal Text"21Reset bold/brightecho -e "Normal \e[1mBold \e[21mNormal"22Reset dimecho -e "Normal \e[2mDim \e[22mNormal"24Reset underlinedecho -e "Normal \e[4mUnderlined \e[24mNormal"25Reset blinkecho -e "Normal \e[5mBlink \e[25mNormal"27Reset reverseecho -e "Normal \e[7minverted \e[27mNormal"28Reset hiddenecho -e "Normal \e[8mHidden \e[28mNormal"
8/16 Colors
The following colors works with most terminals and terminals emulators 2), see the compatibility list for more informations.
NOTE: The colors can vary depending of the terminal configuration.
Foreground (text)
Code Color Example Preview 39Default foreground colorecho -e "Default \e[39mDefault"30Blackecho -e "Default \e[30mBlack"31Redecho -e "Default \e[31mRed"32Greenecho -e "Default \e[32mGreen"33Yellowecho -e "Default \e[33mYellow"34Blueecho -e "Default \e[34mBlue"35Magentaecho -e "Default \e[35mMagenta"36Cyanecho -e "Default \e[36mCyan"37Light grayecho -e "Default \e[37mLight gray"90Dark grayecho -e "Default \e[90mDark gray"91Light redecho -e "Default \e[91mLight red"92Light greenecho -e "Default \e[92mLight green"93Light yellowecho -e "Default \e[93mLight yellow"94Light blueecho -e "Default \e[94mLight blue"95Light magentaecho -e "Default \e[95mLight magenta"96Light cyanecho -e "Default \e[96mLight cyan"97Whiteecho -e "Default \e[97mWhite"
BackgroudCode Color Example Preview 49Default background colorecho -e "Default \e[49mDefault"40Blackecho -e "Default \e[40mBlack"41Redecho -e "Default \e[41mRed"42Greenecho -e "Default \e[42mGreen"43Yellowecho -e "Default \e[43mYellow"44Blueecho -e "Default \e[44mBlue"45Magentaecho -e "Default \e[45mMagenta"46Cyanecho -e "Default \e[46mCyan"47Light grayecho -e "Default \e[47mLight gray"100Dark grayecho -e "Default \e[100mDark gray"101Light redecho -e "Default \e[101mLight red"102Light greenecho -e "Default \e[102mLight green"103Light yellowecho -e "Default \e[103mLight yellow"104Light blueecho -e "Default \e[104mLight blue"105Light magentaecho -e "Default \e[105mLight magenta"106Light cyanecho -e "Default \e[106mLight cyan"107Whiteecho -e "Default \e[107mWhite"
88/256 Colors
Some terminals (see the compatibility list) can support 88 or 256 colors. Here are the control sequences that permit you to use them.
NOTE¹: The colors number 256
is only supported by vte (GNOME Terminal, XFCE4 Terminal, Nautilus Terminal, Terminator,…).
NOTE²: The 88-colors terminals (like rxvt) does not have the same color map that the 256-colors terminals. For showing the 88-colors terminals color map, run the ”256-colors.sh” script in a 88-colors terminal.
Frontgroud(text)
For using one of the 256 colors on the foreground (text color), the control sequence is ”
<Esc>[38;5;
ColorNumberm
” whereColorNumber
is one of the following colors:Examples:
Code (Bash) Preview echo -e "\e[38;5;82mHello \e[38;5;198mWorld"for i in {16..21} {21..16} ; do echo -en "\e[38;5;${i}m#\e[0m" ; done ; echoBackgroudFor using one of the 256 colors on the background, the control sequence is ”
<Esc>[48;5;
ColorNumberm
” whereColorNumber
is one of the following colors:Examples:
Code (Bash) Preview echo -e "\e[40;38;5;82m Hello \e[30;48;5;82m World \e[0m"for i in {16..21} {21..16} ; do echo -en "\e[48;5;${i}m \e[0m" ; done ; echo
Attributes combination
Terminals allow attribute combinations. The attributes must be separated by a semicolon (”;
”).
Examples:
echo -e "\e[1;4mBold and Underlined"Bold + Red forground + Green background
echo -e "\e[1;31;42m Yes it is awful \e[0m"
Terminals compatibility
^[
[6m
” sequence.GNOME Terminalokokok-okokokok-okStrikeout with the ”^[
[9m
” sequence.Guakeokokok-okokokok-okStrikeout with the ”^[
[9m
” sequence.Konsoleok-okokok-okok-ok Nautilus Terminalokokok-okokokok-okStrikeout with the ”^[
[9m
” sequence.rxvtok-ok~ok-okokok-If the background is not set to the default color, Blink make it lighter instead of blinking. Support of italic text with the ”^[
[3m
” sequence.Terminatorokokok-okokokok-okStrikeout with the ”^[
[9m
” sequence.Tildaok-ok-ok-okok--Underline instead of Dim. Convert 256-colors in 16-colors.XFCE4 Terminalokokok-okokokok-okStrikeout with the ”^[
[9m
” sequence.XTermok-okokokokokok-ok xvtok-ok-ok----- Linux TTYok---ok-ok~--Specials colors instead of Dim and Underlined. Lighter background instead of Blink, Bug with 88/256 colors.VTE Terminal 3)okokok-okokokok-okStrikeout with the ”^[
[9m
” sequence.Notations used in the table:
- ”
ok
”: Supported by the terminal. - ”
~
”: Supported in a special way by the terminal. - ”
-
”: Not supported at all by the terminal.
Attributes combination
The following shell script displays a lot of possible combination of the attributes (but not all, because it uses only one formatting attribute at a time).
#!/bin/bash # This program is free software. It comes without any warranty, to# the extent permitted by applicable law. You can redistribute it# and/or modify it under the terms of the Do What The Fuck You Want#Backgroundfor clbg in {40..47} {100..107} 49 ; do#Foregroundfor clfg in {30..37} {90..97} 39 ; do#Formattingfor attr in 0 1 2 4 5 7 ; do#Print the resultecho -en "\e[${attr};${clbg};${clfg}m ^[${attr};${clbg};${clfg}m \e[0m"doneecho #Newlinedonedone exit 0
The following script display the 256 colors available on some terminals and terminals emulators like XTerm and GNOME Terminal.
#!/bin/bash # This program is free software. It comes without any warranty, to# the extent permitted by applicable law. You can redistribute it# and/or modify it under the terms of the Do What The Fuck You Want for fgbg in 38 48 ; do #Foreground/Backgroundfor color in {0..256} ; do #Colors#Display the colorecho -en "\e[${fgbg};5;${color}m ${color}\t\e[0m"#Display 10 colors per linesif [ $((($color + 1) % 10)) == 0 ] ; thenecho #New linefidoneecho #New linedone exit 0
Links
- Linux console codes manual (''man console_codes'')
- XTerm Control Sequences
- Compilation of all escape sequences
- ANSI escape code (Wikipedia)
- Linux Terminal Color
- mac terminal background color
- change dir color in terminal
- [linux]terminal
- linux terminal
- Set terminal text color and transparent background
- Linux Color
- Linux Terminal Shortcut
- Linux Terminal Commands
- linux terminal完整快捷键
- linux terminal 快捷键
- linux下terminal快捷键
- linux新建terminal窗口
- linux上terminal快捷键
- Linux Terminal Command Reference
- Linux Terminal fortune
- linux下terminal快捷键
- Linux Terminal Commands
- java 导出zip包
- python apply函数的用法
- 写在前面的话
- 和东方国际人员天津市人员红地毯
- 采用浮点数编码的遗传算法的问题
- Linux Terminal Color
- IT业地科技体育课的体育课兔耳朵
- sql 查询 in里的个数超过1000 实用方法
- 2012VS在jscript里配置JQuery智能提示
- openFileDialog
- gcc 编译C 报错集中
- 更何况大团圆kit日语课试图科技
- 更何况于公开的愉快的愉快了德育
- 他公开的体育哭团购价法规和是通过何方人士