how to use 256 colors for background color in terminal

In zsh, you don't need to hardcode escape sequences as it has several builtin ways to set the background and foreground colours.

You can use echoti setaf to set the terminal ansi foreground colour and echoti setab to set the background one (setaf and setab being the names of the corresponding terminfo capabilities)

Assuming your terminal supports 256 colours (as VTE-based ones such as your gnome-terminator do) and $TERM is correctly set to a value that identifies a terminfo entry with the right escape sequences for that, it should work.

$ echoti setab 196 | sed -n l
\033[48;5;196m$

Or you can use prompt expansion with print -P or the % parameter expansion flag and:

$ print -rP '%K{196}' | sed -n l
\033[48;5;196m$

(here sed -n l is used to reveal the corresponding escape sequence that is being sent, $ is just to show where the line ends, it's not part of the output, \033 is GNU sed's l command's representation of the ESC character (with octal 033 byte value in ASCII))

Some terminals (including VTE-based ones such as your gnome-terminator) also support RGB specifications. On those, you could do

$ print -rP '%K{#ffffff}' | sed -n l
\033[48;2;255;255;255m$

(here with fffffff for bright white as that's ff the maximum value for all of the red, green and blue components). In that case, zsh hardcodes the xterm-style sequence (see there for the background) as there is no corresponding terminfo capability. Though not standard, that's currently the most widely supported across modern FLOSS terminal emulators.

%K sets the background colour, %F for foreground. %k/%f restore the default colour.

For terminals that don't support that but do support the 88 or 256 colour palette, zsh also has a zsh/nearcolor module to get you the colour nearest to that RGB specification:

$ zmodload zsh/nearcolor
$ echoti colors
256
$ print -rP '%K{#ffffff}' | sed -n l
\033[48;5;231m$

(here colour 231 on my 256 colour terminal is the closest one to bright white, it is actually bright white).

If you have access to the X11 rgb.txt file, you could also define associative arrays for each of the X11 colour names with something like:

typeset -A X11_bg X11_fg
while read -r r g b c; do
  [[ $r = [0-9]* ]] || continue
  printf -v hex %02x $r $g $b
  X11_fg[$c]=${(%):-%F{#$hex}}
  X11_bg[$c]=${(%):-%K{#$hex}}
done < /etc/X11/rgb.txt
X11_bg[default]=${(%):-%k} X11_fg[default]=${(%):-%f}

(Debian-like systems have /etc/X11/rgb.txt as part of the x11-common package).

To do things like:

print -r "$X11_bg[dark olive green]text$X11_bg[default]"

For more details, see:

  • man 5 terminfo
  • info zsh echoti
  • info zsh print
  • info zsh "Prompt Expansion"
  • info zsh "The zsh/nearcolor Module"

(beware that on some systems, you need to install a zsh-doc package or equivalent for the info pages to become available).


printf "\033[48;5;226mhello\e[m\n"

printf "\033[48;2;255;255;0mhello\e[m\n"

The more standard variant of the second (RGB) form (which includes an unused "color space identifier" after the 2) is not supported in older versions of the VTE library (as e.g. libvte-2.91 used by Terminator 1.91 on Debian 10).


The direct colour and indexed colour ITU T.416 control sequences for foreground are SGR 38:2 and SGR 38:5. The control sequences for background are similarly SGR 48:2 and SGR 48:5.

Read § 13.1.8 of ITU T.416.

% TERM=rxvt-256color setterm -7 --background "137" | hexdump -C
00000000  1b 5b 34 38 3b 35 3b 31  33 37 6d                 |.[48;5;137m|
0000000b
%

If you ever progress beyond indexed colour, by the way, you'll need to know that there are, in fact, terminal emulators nowadays that correctly implement SGR 38:2 and SGR 48:2. Two historical mistakes, now being rectified by some people, were not using sub-parameters and forgetting the colour space identifier sub-parameter. iTerm2 fixed this back in 2013; libvte (as used in GNOME Terminator) added support for the : sub-parameters delimiter in 2014, and for the (ignored) color space identifier in 2017.

% TERM=iterm setterm -7 --background "137" | hexdump -C
00000000  1b 5b 34 38 3a 32 3a 3a  31 37 35 3a 31 33 35 3a  |.[48:2::175:135:|
00000010  39 35 6d                                          |95m|
00000014
%

Further reading

  • Control Functions for Coded Character Sets. ECMA-48. 5th edition. 1991. ECMA International.
  • Information technology — Open Document Architecture (ODA) and interchange format: Character content architectures. T.416. International Telecommunication Union.
  • Jonathan de Boyne Pollard (2018). setterm. nosh Guide. Softwares.