How to do: underline, bold, italic, strikethrough, and color in Gnome Terminal?
Bold
Italic
u̲n̲d̲e̲r̲l̲i̲n̲e̲
s̶t̶r̶i̶k̶e̶ ̶i̶t̶ ̶l̶i̶k̶̶e̶ ̶i̶t̶s̶ ̶h̶o̶t
background
font
< (its mono if you couldn't tell)
How to do: underline, bold, italic, strikethrough, and color in Gnome Terminal?
Bold
Italic
u̲n̲d̲e̲r̲l̲i̲n̲e̲
s̶t̶r̶i̶k̶e̶ ̶i̶t̶ ̶l̶i̶k̶̶e̶ ̶i̶t̶s̶ ̶h̶o̶t
background
font
< (its mono if you couldn't tell)
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 character can be obtained with the following syntaxes:
\e
\033
\x1B
The commands (for easy copy-paste):
echo -e "\e[1mbold\e[0m"
echo -e "\e[3mitalic\e[0m"
echo -e "\e[3m\e[1mbold italic\e[0m"
echo -e "\e[4munderline\e[0m"
echo -e "\e[9mstrikethrough\e[0m"
echo -e "\e[31mHello World\e[0m"
echo -e "\x1B[31mHello World\e[0m"
Source (including all types of foreground/background color codes): http://misc.flogisoft.com/bash/tip_colors_and_formatting
/apps/gnome-terminal/profiles/Default/font
)
Commented
Sep 26, 2014 at 19:15
PS1
(in gnome-terminal actually) because underline kind of blended in the text below it making it harder to read, and of course strike-through looked just wrong. Having a line there would help spotting the previous commands when scrolling up (so does color).
GNOME Terminal 3.28 (VTE 0.52), debuting in Ubuntu 18.04 LTS, adds support for a few more styles including curly and colored underlines as seen in Kitty, overline as seen in Konsole, and finally everyone's much loved or much hated blink attribute as well.
GNOME Terminal 3.52 (VTE 0.76), debuting in Ubuntu 24.04 LTS, extends the list with dotted and dashed underline.
These also automatically work in any other VTE-based terminal emulator (e.g. Tilix, Terminator, Xfce4 Terminal, Guake etc.), given that VTE is at least at the said version.
Here's a list demonstrating the standard escape sequences, as well as GNOME Terminal's (VTE's) additions. Note that for every opening sequence I'm also showing the closing sequence of that property only, rather than the generic \e[m
or \e[0m
that disables all special modes.
echo -e '\e[1mbold\e[22m'
echo -e '\e[2mdim\e[22m'
echo -e '\e[3mitalic\e[23m'
echo -e '\e[4munderline\e[24m'
echo -e '\e[4:1mthis is also underline (since 0.52)\e[4:0m'
echo -e '\e[21mdouble underline (since 0.52)\e[24m'
echo -e '\e[4:2mthis is also double underline (since 0.52)\e[4:0m'
echo -e '\e[4:3mcurly underline (since 0.52)\e[4:0m'
echo -e '\e[4:4mdotted underline (since 0.76)\e[4:0m'
echo -e '\e[4:5mdashed underline (since 0.76)\e[4:0m'
echo -e '\e[5mblink (since 0.52)\e[25m'
echo -e '\e[7mreverse\e[27m'
echo -e '\e[8minvisible\e[28m <- invisible (but copy-pasteable)'
echo -e '\e[9mstrikethrough\e[29m'
echo -e '\e[53moverline (since 0.52)\e[55m'
echo -e '\e[31mred\e[39m'
echo -e '\e[91mbright red\e[39m'
echo -e '\e[38:5:42m256-color, de jure standard (ITU-T T.416)\e[39m'
echo -e '\e[38;5;42m256-color, de facto standard (commonly used)\e[39m'
echo -e '\e[38:2::240:143:104mtruecolor, de jure standard (ITU-T T.416) (since 0.52)\e[39m'
echo -e '\e[38:2:240:143:104mtruecolor, rarely used incorrect format (might be removed at some point)\e[39m'
echo -e '\e[38;2;240;143;104mtruecolor, de facto standard (commonly used)\e[39m'
echo -e '\e[46mcyan background\e[49m'
echo -e '\e[106mbright cyan background\e[49m'
echo -e '\e[48:5:42m256-color background, de jure standard (ITU-T T.416)\e[49m'
echo -e '\e[48;5;42m256-color background, de facto standard (commonly used)\e[49m'
echo -e '\e[48:2::240:143:104mtruecolor background, de jure standard (ITU-T T.416) (since 0.52)\e[49m'
echo -e '\e[48:2:240:143:104mtruecolor background, rarely used incorrect format (might be removed at some point)\e[49m'
echo -e '\e[48;2;240;143;104mtruecolor background, de facto standard (commonly used)\e[49m'
echo -e '\e[21m\e[58:5:42m256-color underline (since 0.52)\e[59m\e[24m'
echo -e '\e[21m\e[58;5;42m256-color underline (since 0.52)\e[59m\e[24m'
echo -e '\e[4:3m\e[58:2::240:143:104mtruecolor underline (since 0.52) (*)\e[59m\e[4:0m'
echo -e '\e[4:3m\e[58:2:240:143:104mtruecolor underline (since 0.52) (might be removed at some point) (*)\e[59m\e[4:0m'
echo -e '\e[4:3m\e[58;2;240;143;104mtruecolor underline (since 0.52) (*)\e[59m\e[4:0m'
(*) Truecolor values for underlines are slightly approximated.
And a bit odd one that doesn't quite fit in this picture, as it's more of a functionality than a style, yet is probably worth mentioning here, is hyperlink support co-designed with iTerm2, available since GNOME Terminal 3.26 (VTE 0.50):
echo -e '\e]8;;http://askubuntu.com\e\\hyperlink (since 0.50)\e]8;;\e\\'
To extend Sylvain's answer, some helper functions:
ansi() { echo -e "\e[${1}m${*:2}\e[0m"; }
bold() { ansi 1 "$@"; }
italic() { ansi 3 "$@"; }
underline() { ansi 4 "$@"; }
strikethrough() { ansi 9 "$@"; }
red() { ansi 31 "$@"; }
Then
Something that has not been covered yet is the combination of two or three parameters, e. g. bold and underline, in a predefined color. This is achieved by a 3-way syntax, for instance:
~$ printf "\e[3;4;33mthis is a test\n\e[0m"
will cause "this is a test" to be printed in yellow color (33m
), italic (3m
) AND underlined (4m
).
Note that it is not necessary to repeat the \e[
every time.
Note too that (alike to Sylvain) I also added a \e[0m
to reset settings every time, because otherwise the yellow color and the font style will remain active in terminal! Needless to say that you absolutely have to watch out for these to get reset in scripts, because users who use your scripts may dislike it if your script permanently modifies their color + style settings in terminal!
Replace these hard-coded sequences by:
tput smul # set underline
tput rmul # remove underline
tput smso # set bold on
tput rmso # remove bold
tput setaf 1 #red
tput setaf 2 #green
...
tput cup 0 0 # move to pos 0,0
Refer to "man terminfo" and "man tput" for complete descriptions of these commands.
Example :
function f_help
{
c_green=$(tput setaf 2 2>/dev/null)
c_reset=$(tput sgr0 2>/dev/null)
c_bold=$(tput smso 2>/dev/null)
echo "${c_bold}DESCRIPTION${c_reset} : .... ${c_green}My green text${c_reset}My plain text"
}
smso
sets standout
not bold
. standout
is usually the combination of reverse and bold. There is no standard tput/terminfo/termcap capability for turning off bold
, however ECMA-48:1991 defines SGR 22 as "normal colour or normal intensity (neither bold nor faint)" so you can reasonably use echo -ne "\\033[22m"
in bash
.
echo -e