Jay Taylor's notes
back to listing indexbash:tip_colors_and_formatting - FLOZz' MISC
[web search]FLOZz' MISC » bash:tip_colors_and_formatting
Bash tips: Colors and formatting (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:
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.
Formatting
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 |
---|---|---|---|
1 | Bold/Bright | echo -e "Normal \e[1mBold" | |
2 | Dim | echo -e "Normal \e[2mDim" | |
4 | Underlined | echo -e "Normal \e[4mUnderlined" | |
5 | Blink 1) | echo -e "Normal \e[5mBlink" | |
7 | Reverse (invert the foreground and background colors) | echo -e "Normal \e[7minverted" | |
8 | Hidden (usefull for passwords) | echo -e "Normal \e[8mHidden" |
Reset
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)
Background
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.
Foreground (text)
For using one of the 256 colors on the foreground (text color), the control sequence is
”<Esc>[38;5;
ColorNumberm
” where ColorNumber
is one of the following colors:
Examples:
Background
For using one of the 256 colors on the background, the control sequence is
”<Esc>[48;5;
ColorNumberm
” where ColorNumber
is one of the following colors:
Examples:
Attributes combination
Terminals allow attribute combinations. The attributes must be separated by a semicolon (”;
”).
Examples:
Terminals compatibility
Terminal | Formatting | Colors | Comment | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Bold | Dim | Underlined | Blink | invert | Hidden | 8 | 16 | 88 | 256 | ||
aTerm | ok | - | ok | - | ok | - | ok | ~ | - | - | Lighter background instead of blink. |
Eterm | ~ | - | ok | - | ok | - | ok | ~ | - | ok | Lighter color instead of Bold. Lighter background instead of blink. Can overline a text with the ”^[ [6m ” sequence. |
GNOME Terminal | ok | ok | ok | - | ok | ok | ok | ok | - | ok | Strikeout with the ”^[ [9m ” sequence. |
Guake | ok | ok | ok | - | ok | ok | ok | ok | - | ok | Strikeout with the ”^[ [9m ” sequence. |
Konsole | ok | - | ok | ok | ok | - | ok | ok | - | ok | |
Nautilus Terminal | ok | ok | ok | - | ok | ok | ok | ok | - | ok | Strikeout with the ”^[ [9m ” sequence. |
rxvt | ok | - | ok | ~ | ok | - | ok | ok | ok | - | 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. |
Terminator | ok | ok | ok | - | ok | ok | ok | ok | - | ok | Strikeout with the ”^[ [9m ” sequence. |
Tilda | ok | - | ok | - | ok | - | ok | ok | - | - | Underline instead of Dim. Convert 256-colors in 16-colors. |
XFCE4 Terminal | ok | ok | ok | - | ok | ok | ok | ok | - | ok | Strikeout with the ”^[ [9m ” sequence. |
XTerm | ok | - | ok | ok | ok | ok | ok | ok | - | ok | |
xvt | ok | - | ok | - | ok | - | - | - | - | - | |
Linux TTY | ok | - | - | - | ok | - | ok | ~ | - | - | Specials colors instead of Dim and Underlined. Lighter background instead of Blink, Bug with 88/256 colors. |
VTE Terminal 3) | ok | ok | ok | - | ok | ok | ok | ok | - | ok | Strikeout 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.
Demonstration programs
Colors and formatting (16 colors)
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).
- colors_and_formatting.sh
#!/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 # To Public License, Version 2, as published by Sam Hocevar. See # http://sam.zoy.org/wtfpl/COPYING for more details. #Background for clbg in {40..47} {100..107} 49 ; do #Foreground for clfg in {30..37} {90..97} 39 ; do #Formatting for attr in 0 1 2 4 5 7 ; do #Print the result echo -en "\e[${attr};${clbg};${clfg}m ^[${attr};${clbg};${clfg}m \e[0m" done echo #Newline done done exit 0
256 colors
The following script display the 256 colors available on some terminals and terminals emulators like XTerm and GNOME Terminal.
- 256-colors.sh
#!/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 # To Public License, Version 2, as published by Sam Hocevar. See # http://sam.zoy.org/wtfpl/COPYING for more details. for fgbg in 38 48 ; do #Foreground/Background for color in {0..256} ; do #Colors #Display the color echo -en "\e[${fgbg};5;${color}m ${color}\t\e[0m" #Display 10 colors per lines if [ $((($color + 1) % 10)) == 0 ] ; then echo #New line fi done echo #New line done exit 0
Links
Discussion
if [[ ${EUID} == 0 ]] ; then
PS1='\e[1;31;48;5;234m\u \e[38;5;240mon \e[1;38;5;28;48;5;234m\h \e[38;5;54m\d \@\e[0m\n\e[0;31;48;5;234m[\w] \e[1m\$\e[0m '
else
PS1='\e[1;38;5;56;48;5;234m\u \e[38;5;240mon \e[1;38;5;28;48;5;234m\h \e[38;5;54m\d \@\e[0m\n\e[0;38;5;56;48;5;234m[\w] \e[1m\$\e[0m '
fi
@caravaggisto
I was actually looking to find out if there is a way to combine attributes {BOLD, Blink, etc} around the same subset of text in doing a bash echo command with the -e option.
Can you help with this matter?
\\War
You can combine attributes with a semicolon:
echo -e "\e[1;5m Bold+Blink \e[0m"
echo -e "\e[1;4;31m Bold+Underline+Red \e[0m"
Note that the blink attribute is supported only by few terminals (XTerm, tty).
Regards,
konrad@vps1 ~/web/abc▌▌▌mkdir xyz
PS1='\[\e[0m\]\[\e[48;5;236m\]\[\e[38;5;105m\]\u\[\e[38;5;105m\]@\[\e[38;5;105m\]\h\[\e[38;5;105m\] \[\e[38;5;221m\]\w\[\e[38;5;221m\]\[\e[38;5;105m\]\[\e[0m\]\[\e[38;5;236m\]\342\226\214\342\226\214\342\226\214\[\e[0m\]'
root:
PS1='\[\e[0m\]\[\e[48;5;236m\]\[\e[38;5;197m\]\u\[\e[38;5;197m\]@\[\e[38;5;197m\]\h\[\e[38;5;105m\] \[\e[38;5;221m\]\w\[\e[38;5;221m\]\[\e[38;5;105m\]\[\e[0m\]\[\e[38;5;236m\]\342\226\214\342\226\214\342\226\214\[\e[0m\]'
© 2011 Fabien LOISON - www.flogisoft.com