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[41m (red bg, 40–47)\033[41m\e[41mESC [ 4 1 m1b 5b 34 31 6dLive preview
Rendered in your browser via the same tokeniser the decoder uses — no terminal needed.
Description
The 8 basic background colors mirror 30–37 at parameters 40–47: 40 black, 41 red, 42 green, 43 yellow, 44 blue, 45 magenta, 46 cyan, 47 white. SGR 49 returns the background to the terminal default — the surgical complement to a full \x1b[0m reset. ECMA-48 §8.3.117 specifies the range; every terminal in the support matrix above honours the full surface, including the Linux console (8-colour mode) and ConPTY. Like the foreground, the rendered RGB depends entirely on the terminal colour scheme — there is no canonical 'ANSI red bg' — and the user's theme can remap any of the 8 slots arbitrarily. The bright background variants live at SGR 100–107 (aixterm extension, see sgr-bg-bright); for indexed 256-colour backgrounds use \x1b[48;5;Nm, for 24-bit RGB use \x1b[48;2;R;G;Bm. Combine basic bg with basic fg in one sequence by chaining parameters: \x1b[31;47m is red-on-white (semicolons are separators, not a sequence ender).
Two background-specific traps land more often than fg ones. (1) Background colour pads cells: when you don't reset the bg before emitting a newline, the bg fill carries to the end of the line on most terminals — your 'red label' turns into a long red bar past the text. Always emit \x1b[49m (bg-off) or \x1b[0m before \n. (2) Naïve width math breaks alignment when you add colour: printf '%-20s' and len(string) count the 4–7 SGR bytes that don't print, so coloured cells look shorter than uncoloured ones. The fix is to strip ANSI first and measure visible width separately — TUI libraries (rich, blessed, lipgloss, ratatui) handle this for you, but raw printf does not. And as with all colour output: gate emission on isatty(stdout) and honour NO_COLOR=1; without those, scripts that look fine on dev leak SGR bytes into log files and CI captures (see no-color). On the Linux console / fbcon the basic 16-bg surface is the only background that renders — \x1b[48;5;Nm and \x1b[48;2;R;G;Bm silently quantize to the nearest of those slots.
Disable background surgically with \x1b[49m — leaves foreground and attributes intact. The pair \x1b[41m…\x1b[49m is the right shape; reaching for \x1b[0m clobbers more than you intended. For finer control beyond the 8 slots see sgr-bg-256 (256-colour palette) or sgr-bg-truecolor (24-bit RGB) — both inherit the same 'reset before newline' gotcha. For the matching foreground side see sgr-fg-basic; for the bright background variants that aixterm-style terminals also expose see sgr-bg-bright. If you want a textual reset that also clears every other SGR attribute you set in the same span, sgr-reset (\x1b[0m) is the broad close.
Spec citation: ECMA-48 §8.3.117 (SGR parameters 40–47, 49)
Examples
for c in 40 41 42 43 44 45 46 47; do printf "\033[${c}m \033[0m"; done; echofor c in range(40, 48): print(f'\x1b[{c}m \x1b[0m', end='')for c := 40; c <= 47; c++ { fmt.Printf("\x1b[%dm \x1b[0m", c) }for (let c = 40; c <= 47; c++) process.stdout.write(`\x1b[${c}m \x1b[0m`)for (int c = 40; c <= 47; c++) printf("\x1b[%dm \x1b[0m", c);Used in
Real-world tools that emit this sequence — anchors the bytes to commands you've already used.
- mansection-heading background (NAME, SYNOPSIS) on some pagers when LESS_TERMCAP_so is set
- lessstatus line background on --status-column
- vim, neovimStatusLine, SignColumn, Search highlight groups
- git diff --color-moved (alt-bg on moved blocks)
- htop, btop column header rows
Frequently asked
Short answers to the questions developers actually search for this sequence.
- Why does
\x1b[41mnot look red on macOS Terminal default profile? - SGR 40–47 don't carry RGB — they're palette indices 0–7, and the terminal's profile decides what each slot actually paints. macOS Terminal's default Basic profile maps slot 1 (red) to a muted maroon (
#990000-ish) for legibility on a white background; iTerm2 Solarized andxtermdefaults paint slot 1 noticeably brighter. If you need a stable hue across user profiles, pick truecolor instead —\x1b[48;2;255;0;0m— seesgr-bg-truecolor. Never assume 40–47 means a specific RGB. - Does
\x1b[49mreset just the background, or the whole SGR state? - Background only. SGR 49 sets background to the terminal's default; foreground, bold, italic, underline, etc. are untouched — see
sgr-default-bg. For a clean wipe of every attribute use SGR 0 (\x1b[0m); for narrower disables compose them (\x1b[39;49mclears both colours but keeps weight + underline). Treat 49 like a try/finally close for the background channel, paired with whatever opened it.
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
- yes
- GNU screen
- yes
| xterm | Linux console (fbcon) | macOS Terminal.app | iTerm2 | Windows Terminal | cmd.exe / ConPTY | kitty | alacritty | WezTerm | Ghostty | GNOME Terminal | Konsole | tmux | GNU screen |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| yes | yes | yes | yes | yes | yes | yes | yes | yes | yes | yes | yes | yes | yes |