70

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

  • 1
    And what exactly want to change? The prompt, the text you enter, the output of your commands? – Radu Rădeanu Sep 26 '14 at 16:43
  • export PS1 actually. Although I'd like it for basic echo as well. – Akiva Sep 26 '14 at 18:41
  • 1
    If it supported "Faint", I'd switch. :( – Jürgen A. Erhard Jan 22 '16 at 21:08
  • You can use echo -e – Natim Apr 2 at 14:52
92

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

| improve this answer | |
  • 1
    @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) – Sylvain Pineau Sep 26 '14 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. – Sylvain Pineau May 27 '16 at 12:22
  • 1
    @sherrellbc a delimiter indeed (The "m" terminates each term of the escape sequence, according to tldp.org/LDP/abs/html/colorizing.html) – Sylvain Pineau Oct 13 '16 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 Dec 6 '17 at 17:22
  • 1
    @dashesy Thanks, it's actually a pretty cool use case :) Gnome-terminal progress is tracked here. – egmont Dec 6 '17 at 19:06
30

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

25

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!

| improve this answer | |
23

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.

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 version 0.52.

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 (new in 0.52)\e[4:0m'
echo -e '\e[21mdouble underline (new in 0.52)\e[24m'
echo -e '\e[4:2mthis is also double underline (new in 0.52)\e[4:0m'
echo -e '\e[4:3mcurly underline (new in 0.52)\e[4:0m'
echo -e '\e[5mblink (new in 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 (new in 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) (new in 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) (new in 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 (new in 0.52)\e[59m\e[24m'
echo -e '\e[21m\e[58;5;42m256-color underline (new in 0.52)\e[59m\e[24m'
echo -e '\e[4:3m\e[58:2::240:143:104mtruecolor underline (new in 0.52) (*)\e[59m\e[4:0m'
echo -e '\e[4:3m\e[58:2:240:143:104mtruecolor underline (new in 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 (new in 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\e]8;;\e\\'

Here's a screenshot demonstrating the result: Rendering in gnome-terminal 3.28

| improve this answer | |
  • 2
    A screenshot of the result would be lovely, for those of us who don't have these gnome-terminal/vte versions. – Marius Gedminas Apr 16 '18 at 20:11
  • @MariusGedminas Done. – egmont Apr 16 '18 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 Jul 31 '19 at 0:10
4

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"
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.