98

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

Color

background

font < (its mono if you couldn't tell)

size

4
  • 1
    And what exactly want to change? The prompt, the text you enter, the output of your commands? Commented Sep 26, 2014 at 16:43
  • export PS1 actually. Although I'd like it for basic echo as well.
    – Anon
    Commented Sep 26, 2014 at 18:41
  • 1
    If it supported "Faint", I'd switch. :( Commented Jan 22, 2016 at 21:08
  • You can use echo -e
    – Natim
    Commented Apr 2, 2020 at 14:52

5 Answers 5

129

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

enter image description here

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

18
  • 2
    @Akiva You can easily change the background color (See the dedicated section). Regarding size, I don't think it's possible. For font, the only setting is a global gconf value (/apps/gnome-terminal/profiles/Default/font) Commented Sep 26, 2014 at 19:15
  • 1
    @dashesy: According to en.wikipedia.org/wiki/ANSI_escape_code, 53 should do the overline. But in my terminal is does nothing. Commented May 27, 2016 at 12:22
  • 2
    @sherrellbc a delimiter indeed (The "m" terminates each term of the escape sequence, according to tldp.org/LDP/abs/html/colorizing.html) Commented Oct 13, 2016 at 15:32
  • 1
    @egmont I remember I wanted to use it in 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).
    – dashesy
    Commented Dec 6, 2017 at 17:22
  • 1
    @dashesy Thanks, it's actually a pretty cool use case :) Gnome-terminal progress is tracked here.
    – egmont
    Commented Dec 6, 2017 at 19:06
49

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\\'

Here's a screenshot demonstrating the result: Rendering in GNOME Terminal 3.52

4
  • 3
    A screenshot of the result would be lovely, for those of us who don't have these gnome-terminal/vte versions. Commented Apr 16, 2018 at 20:11
  • 1
    @MariusGedminas Done.
    – egmont
    Commented Apr 16, 2018 at 20:57
  • 1
    Awesome answer! Thanks for taking the time to collect all these! I wonder if they also work on iTerm2 (will try to check that for myself shortly...)
    – filbranden
    Commented Jul 31, 2019 at 0:10
  • Sorry for being late to the party, but, it works on iTerm2 as of v3.5.0 (May 2024). Commented May 22 at 6:08
43

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

enter image description here

1
  • 2
    echo "$(bold "this doesn't $(italic really) work for all purposes though")" Commented Apr 21, 2021 at 7:20
29

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!

0
16

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"
}
2
  • Doing it in Python: stackoverflow.com/questions/6199285/…
    – user
    Commented Nov 19, 2020 at 19:59
  • 4
    Note that 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.
    – Old Pro
    Commented Dec 12, 2020 at 20:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .