112

I can change the colour through preference , but how can I change the background colour and text colour of terminal through the command line?

muru
  • 197,895
  • 55
  • 485
  • 740
sigdelsanjog
  • 7,110

5 Answers5

79

On certain XTerm/ANSI-compatible terminals (like xterm and gnome-terminal), you can set colors using a palette bigger then the default 8/16-colors palette (for example using an 88-colors, 256-colors or 16777216-colors (RGB) palette; both xterm and gnome-terminal support both the 256-colors and the 16777216-colors (RGB) palette); mind that the shell might override this (this is the case e.g. for zsh).

Here's a script to list the colors in the 256-color palette along with their ANSI color code in XTerm/ANSI-compatible terminals with a 256-color palette support:

#!/bin/bash
for((i=16; i<256; i++)); do
    printf "\e[48;5;${i}m%03d" $i;
    printf '\e[0m';
    [ ! $((($i - 15) % 6)) -eq 0 ] && printf ' ' || printf '\n'
done

screenshot1

screenshot

Depending on whether you want to apply the color to the foreground or to the background, use an <fg_bg> value of 38 or 48 (respectively) in the following command:

printf '\e[<fg_bg>;5;<ANSI_color_code>m'

For example, to set the foreground color (<fg_bg>=38) to red (<ANSI_color_code>=196) and the background color (<fg_bg>=48) to black (<ANSI_color_code>=0):

printf '\e[38;5;196m Foreground color: red\n'
printf '\e[48;5;0m Background color: black\n'

screenshot3

It's necessary to redraw the prompt using printf '\e[K' in order for the background color to apply to the whole line and in order for the foreground color to apply to the cursor:

screenshot4

The same thing can be accomplished using RGB values instead of ANSI color codes in a compatible terminal; depending on whether you want to apply the color to the foreground or to the background, use an <fg_bg> value of 38 or 48 (respectively) in the following command:

printf '\e[<fg_bg>;2;<R>;<G>;<B>m'

For example, to set the foreground color (<fg_bg>=38) to red (<R>=255, <G>=0, <B>=0) and the background color (<fg_bg>=48) to black (<R>=0, <G>=0, <B>=0):

printf '\e[38;2;255;0;0m Foreground color: red\n'
printf '\e[48;2;0;0;0m Background color: black\n'

screenshot5

Again, it's necessary to redraw the prompt using printf '\e[K' in order for the background color to apply to the whole line and in order for the foreground color to apply to the cursor:

screenshot6

Using either methods, you can use printf '\e[0m' to reset all the attributes:

screenshot7

kos
  • 35,891
  • 2
    Both xterm and gnome-terminal recognize the \e[38/48;2;R;G;Bm true color escape sequences; however, xterm rounds the actual color to the nearest in its 256-color palette. gnome-terminal displays the exact true color given in the escape sequence. – egmont Feb 13 '16 at 08:34
  • 2
    \e[K is dangerous; in some emulators (e.g. xterm) if the cursor happens to be at the very right edge (visually displayed in the rightmost column, but logically already beyond that since a character was already printed in the rightmost column), that last letter gets stripped. See e.g. https://bugzilla.gnome.org/show_bug.cgi?id=740789 or http://savannah.gnu.org/bugs/?36831. – egmont Feb 13 '16 at 08:41
  • Correcting myself: If you emit \e[K at the beginning of the line (at the beginning of the prompt, rather than at the end of it) then of course it's safe. (Won't work with multiline prompt or command though.) – egmont Feb 13 '16 at 08:44
  • @egmont Thanks for the useful information, I see that on GNOME Bugzilla that is marked as fixed, is this the case? I can't test this at this very moment, so I'll do that later to check if this is still the case (unless you reply first) and include that information in the answer. – kos Feb 13 '16 at 10:17
  • Yup newest versions of gnome-terminal don't strip that character. Whether this is the correct behavior is questionable. After all, graphical terminal emulators are emulating some ancient devices. Should we emulate their bad design decisions (as xterm does here), or should we override (as we do in gnome-terminal)? It's a tough question with no single correct answer. – egmont Feb 13 '16 at 14:35
  • 1
    No, there is actually a correct answer - the simulation is NOT to reproduce bugs and illogical behaviour. – shevy May 18 '19 at 16:20
52

Information as found on this page, excluding preview column:

Sequences are composed of the Escape character (often represented by ”^[” or ”<Esc>”) followed by some other characters: ”^[FCm” (where FC is one of the numbers in the bulleted list below).

In bash, the Esc code can be either of the following:

  1. \e
  2. \033 (octal)
  3. \x1B (hexadecimal)

Note 1: 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 2: Foreground and background colours may vary, depending on the terminal's configuration and not all colours are supported.

Set/Reset

  • 0: Reset/remove all modifier, foreground and background attributes: echo -e "\e[0mNormal Text"
  • 1: Bold/Bright: echo -e "Normal \e[1mBold"
  • 2: Dim: echo -e "Normal \e[2mDim"
  • 4: Underlined: echo -e "Normal \e[4mUnderlined"
  • 5: Blink (doesn't work in most terminals except XTerm): echo -e "Normal \e[5mBlink"
  • 7: Reverse/Invert: echo -e "Normal \e[7minverted"
  • 8: Hidden (useful for sensitive info): echo -e "Normal \e[8mHidden Input"
  • 21: Reset/Remove bold/bright: echo -e "Normal \e[1mBold \e[21mNormal"
  • 22: Reset/Remove dim: echo -e "Normal \e[2mDim \e[22mNormal"
  • 24: Reset/Remove underline: echo -e "Normal \e[4mUnderlined \e[24mNormal"
  • 25: Reset/Remove blink: echo -e "Normal \e[5mBlink \e[25mNormal"
  • 27: Reset/Remove reverse/invert: echo -e "Normal \e[7minverted \e[27mNormal"
  • 28: Reset/Remove hidden: echo -e "Normal \e[8mHidden \e[28mNormal"

Foreground

  • 39: Default (usually green, white or light gray): echo -e "Default \e[39mDefault"
  • 30: Black: echo -e "Default \e[30mBlack" (best combined with a background colour: echo -e "Default \e[30;107mBlack on white")
  • 31: Red (don't use with green background)
  • 32: Green
  • 33: Yellow
  • 34: Blue
  • 35: Magenta/Purple
  • 36: Cyan
  • 37: Light Gray
  • 90: Dark Gray
  • 91: Light Red
  • 92: Light Green
  • 93: Light Yellow
  • 94: Light Blue
  • 95: Light Magenta/Pink
  • 96: Light Cyan
  • 97: White

Background

  • 49: Default background color (usually black or blue)
  • 40: Black
  • 41: Red
  • 42: Green
  • 43: Yellow
  • 44: Blue
  • 45: Magenta/Purple
  • 46: Cyan
  • 47: Light Gray (don't use with white foreground)
  • 100: Dark Gray (don't use with black foreground)
  • 101: Light Red
  • 102: Light Green (don't use with white foreground)
  • 103: Light Yellow (don't use with white foreground)
  • 104: Light Blue (don't use with light yellow foreground)
  • 105: Light Magenta/Pink (don't use with light foreground)
  • 106: Light Cyan (don't use with white foreground)
  • 107: White (don't use with light foreground)

To set both the foreground and background colours at once, use ther form echo -e "\e[S;FG;BGm". For example: echo -e "\e[1;97;41m" (bold white foreground on red background)

For 256 colour options, see the source page.

Agi Hammerthief
  • 639
  • 5
  • 11
32

Change the colors on an incidental basis

If it is meant to change colors on an incidental basis:

You can use the setterm command:

setterm -term linux -back <background_colour> -fore <text_color> -clear

from the colors, you can chose from (both fore- and background):

black|blue|green|cyan|red|magenta|yellow|white|default

for more options:

setterm -help

Change your profile (color) settings

In 14.04, I didn't find an option to use dconf to set the colors or the terminal. You can however use gconftool

  • You first need to get your profile name:

    gconftool-2 --get /apps/gnome-terminal/global/profile_list
    
  • Then, to set the text colors of your profile:

    gconftool-2 --set "/apps/gnome-terminal/profiles/<profile_name>/foreground_color" --type string "#FFFFFF"
    

    for example to set the text color to white

    The same with background color:

    gconftool-2 --set "/apps/gnome-terminal/profiles/<profile_name>/background_color" --type string "#000000"
    

    for example to set the background color to black

ALternatively, to set the name of the color(s), you can simply use white or green, from the same palette as the setterm command, e.g.:

gconftool-2 --set "/apps/gnome-terminal/profiles/<profile_name>/background_color" --type string black
Jacob Vlijm
  • 83,767
  • 1
    Those should be double dashes in the initial setterm command. – user1770201 Nov 13 '15 at 15:01
  • Worked on 14.04, but not on 16.04 (beta; not sure if that's the reason)... Still, dconf write /org/gnome/terminal/legacy/profiles:/:<profile_id>/background-color "'rgb(0,0,0)'" did the trick. – Janaka Bandara Apr 17 '16 at 17:50
  • Not working on ubuntu 1704, it looks bad – Wax Cage Aug 07 '17 at 22:29
  • 1
    gconftool-2 (for Ubuntu 14.04 and maybe a few following releases), or dconf or gsettings on newer versions of Ubuntu change gnome-terminal's settings, and take immediate effect on all the tabs and windows of the same profile. I don't think OP was looking for this behavior. – egmont May 09 '19 at 13:44
  • 1
    The first command returen this "setterm: option '-clear' requires an argument" – thanos.a Apr 20 '20 at 21:11
11

See kos's answer for switching the foreground or background to a particular color of the palette, or even a direct RGB color in some terminals. The effect of such a sequence lasts until a different color is selected (or it's reverted to the default).

Another approach, supported by some terminal emulators, is to redefine the exact RGB values of the terminal's default foreground and background colors using the OSC 10 / 11 escape sequences:

echo -ne '\e]10;#123456\e\\'  # set default foreground to #123456
echo -ne '\e]11;#abcdef\e\\'  # set default background to #abcdef

This is likely to last much longer (until overridden by another OSC 10 / 11 or reset via OSC 110 / 111), but unlike the dconf approach, this still affects the current terminal only.

dessert
  • 39,982
egmont
  • 8,225
5

The various colour codes that used for obtaining coloured output can also be used to obtain coloured backgrounds:

40  black
41  red
42  green
43  yellow
44  blue
45  magenta
46  cyan
47  white

Therefore, the following command turns my background red:

$ echo -e '\e[0;41m'

Depending on the shell, the terminal emulator, etc., you might not need the -e.

muru
  • 197,895
  • 55
  • 485
  • 740