Skip to main content
ansicode

SGR 40–47 — Background color (8 basic)

Set background to one of black/red/green/yellow/blue/magenta/cyan/white.

Byte forms

Every common string-literal form so you can paste-and-search either direction.

\\x1b[\x1b[41m (red bg, 40–47)
\\033[\033[41m
\\e[\e[41m
ESC [ESC [ 4 1 m
hex1b 5b 34 31 6d

Description

Background equivalents of 30–37. 40 black, 41 red, 42 green, 43 yellow, 44 blue, 45 magenta, 46 cyan, 47 white. SGR 49 resets the background to the terminal default. Like the foreground, the rendered RGB depends on the terminal color scheme.

Spec citation: ECMA-48 §8.3.117 (SGR parameters 40–47, 49)

Examples

bash
for c in 40 41 42 43 44 45 46 47; do printf "\033[${c}m  \033[0m"; done; echo
python
for c in range(40, 48): print(f'\x1b[{c}m  \x1b[0m', end='')
go
for c := 40; c <= 47; c++ { fmt.Printf("\x1b[%dm  \x1b[0m", c) }
javascript
for (let c = 40; c <= 47; c++) process.stdout.write(`\x1b[${c}m  \x1b[0m`)
c
for (int c = 40; c <= 47; c++) printf("\x1b[%dm  \x1b[0m", c);

Terminal support

xterm
yes
Linux console (fbcon)
yes
macOS Terminal.app
yes
iTerm2
yes
Windows Terminal
yes
cmd.exe / ConPTY
yes
kitty
yes
alacritty
yes
WezTerm
yes
Ghostty
yes
GNOME Terminal
yes
Konsole
yes
tmux
no
GNU screen
no

Related sequences

In the family cookbook

SGR cookbook · 2. 16-color basic — `\x1b[31m` and friends