Skip to main content
ansicode

SGR 38;5;n — 256-color foreground

Pick a foreground color from the 256-color xterm palette.

Byte forms

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

\\x1b[\x1b[38;5;Nm
\\033[\033[38;5;Nm
\\e[\e[38;5;Nm
ESC [ESC [ 3 8 ; 5 ; N m
hex1b 5b 33 38 3b 35 3b <N> 6d

Description

N is 0–255. 0–7 map to the 8 basic colors, 8–15 to the bright variants, 16–231 form a 6×6×6 RGB cube (`16 + 36*r + 6*g + b` for r,g,b in 0–5), and 232–255 form a 24-step grayscale ramp. Background equivalent: `\x1b[48;5;Nm`. Older subterminals also accept the colon form `\x1b[38:5:Nm` (per ECMA-48 sub-parameter separator).

Spec citation: xterm-ctlseqs (256-color extension)

Examples

bash
for n in 16 51 196 226 51 21 201; do printf "\033[38;5;${n}m■\033[0m"; done; echo
python
for n in [16, 196, 226, 21, 51, 201]: print(f'\x1b[38;5;{n}m■\x1b[0m', end='')
go
for _, n := range []int{16,196,226,21,51,201} { fmt.Printf("\x1b[38;5;%dm■\x1b[0m", n) }
javascript
[16,196,226,21,51,201].forEach(n => process.stdout.write(`\x1b[38;5;${n}m■\x1b[0m`))
c
int n[] = {16,196,226,21,51,201}; for (int i=0;i<6;i++) printf("\x1b[38;5;%dm■\x1b[0m", n[i]);

Terminal support

xterm
yes
Linux console (fbcon)
no
macOS Terminal.app
yes
iTerm2
yes
Windows Terminal
yes
cmd.exe / ConPTY
partial
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 · 3. 256-color indexed — `\x1b[38;5;n m`