Note: I reworked the question to clarify the issues. This is a trivial question that can be answered in several parts because the question has several attributes.

Unfortunately, the question may be reworked when the person asking the question apprehends (knows) some parts of the answer as well as the structure of the question.

Therefore, the question is presented from a different point of view than the original one.

How to change the color setting¹ of the Linux console, using a shell utility? How to change the color setting¹ of a terminal emulator, using a shell utility?

Originally, I wanted to change the background color of the Linux console during the execution of a shell script. The following proposition works as I wanted.


# set the background color of the terminal
tput setab 6

printf "%s\n" 'background: cyan'

# do some tasks 
cd /usr/src/initramfs
ls -l

# the background of the shell output is cyan

Nonetheless, someone may encounter several glitches (or difficulties).

For instance, if the screen is not cleared, the background color is only applied to the text output, during a shell script execution. If the command tput setab 6 is submitted with the command line and the screen is not cleared, the background color is only applied to a small portion of the output text. If the command tput setab 6 is submitted on the command line and the screen is cleared, the background color is applied to the full screen but only persists until another command is submitted. [...]

¹ background color


On the linux virtual terminal ("console"):

To set the background to color 1 (red):

printf %b '\e[41m' '\e[8]' '\e[H\e[J'

The first escape (setab) sets the background to color 1, the second escape defines the current foreground-background pair as the default, and the third (clear) clears the screen.

From now on, 'red' will be default background:

printf %b 'World\e[42mWide\e[mWeb\n'

Notice how setting the background to green with setab 2 (\e[42) and subsequent clearing of attributes with sgr0 (\e[m) do not wipe off our setting.

Alternatively, you can redefine what color 0 (the default background, black) means via the "set palette" (aka initc = "initialize_color") escape:

printf %b '\e[40m' '\e[8]' # set default background to color 0 (black)
printf %b '\e]P0ff0000'    # redefine color 0 as 'red'

The latter takes an argument of the form nRRGGBB, where n is the color number, and RRGGBB is a hex triplet defining the color.

This escape is also supported by putty.

For an explanation of these functions and more, check the console_codes(4) and terminfo(5) manpages.

I wasn't able to either:

a) change the background color of the screen without wiping everything from it with clear.

b) force it to update the non-character margin of the screen without switching back-and-forth between virtual terminals with Control-Alt-Fn.

On xterm-like terminal emulators:

This sets the background to a light green without having to clear the screen:

printf %b '\e]11;#ccffcc\a'

It also works fine on mlterm, rxvt, gnome-terminal, konsole etc.

The alternative form \e]11;rgb:cc/ff/cc\a (taken from here) is not supported in konsole (tested with with 12.16.0 version).

This will not work inside tmux or screen.

All of xterm's escapes are documented in the ctlseqs.txt file from its source code.


Some of the escapes (eg. \e[8] or \e]11;...\a) are not included in terminfo and cannot be used with tput at all, while others (eg. initc) are too awkward to be used in a clean way.

A greppable version of the terminfo database could be obtained by a command like: find /usr/share/terminfo -type f -printf '%f\n' | xargs -n1 infocmp -0 -A /usr/share/terminfo

  • The manual page console_codes (4) says: "It is generally not a good practice to hard-wire terminal controls into programs. Linux supports a terminfo (5) database of terminal capabilities. Rather than emitting console escape sequences by hand, you will almost want to use a terminfo-aware screen library or utility such as ncurses (5), tput (1), or reset(1)". – Fólkvangr Oct 12 '18 at 7:55
  • I already addressed that in the last paragraph of my answer. – mosvy Oct 12 '18 at 7:59
  • 1
    besides, the interface to initc is quite idiotic, because you have to split and transform an RGB triplet from the standard 0-255 range to 0-999 (or 0-499!), just to have it transformed back to 0-255 by tput :-) – mosvy Oct 12 '18 at 8:14
  • Do you say that the terminfo database is not standard? We have the same questions about the process. It is likely that the method depends on the operation of the virtual terminal despite some abstractions. – Fólkvangr Oct 13 '18 at 9:08
  • 1
    Some of that's incorrect: whether the escapes are standard or not is irrelevant. Their functions do not correspond to predefined features in terminfo. If someone thought they were useful enough, they could be added to a terminal description. However, they would not be useful with curses applications (without adding functions to exploit the features). – Thomas Dickey Oct 13 '18 at 10:54

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.