Skip to main content
ansicode

SGR 30–37 — Foreground color (8 basic)

Set foreground 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[31m (red, similarly 30–37)
\\033[\033[31m
\\e[\e[31m
ESC [ESC [ 3 1 m
hex1b 5b 33 31 6d

Description

The 8 basic foreground colors: 30 black, 31 red, 32 green, 33 yellow, 34 blue, 35 magenta, 36 cyan, 37 white. SGR 39 resets the foreground to the terminal default without touching the background. The actual rendered RGB is theme-dependent — there is no canonical RGB for 'ANSI red'. To pick a specific RGB, use truecolor (SGR 38;2;…).

Spec citation: ECMA-48 §8.3.117 (SGR parameters 30–37, 39)

Parameters

30black
31red
32green
33yellow
34blue
35magenta
36cyan
37white
39default foreground

Examples

bash
for c in 30 31 32 33 34 35 36 37; do printf "\033[${c}m■\033[0m"; done; echo
python
for c in range(30, 38): print(f'\x1b[{c}m{c}\x1b[0m', end=' ')
go
for c := 30; c <= 37; c++ { fmt.Printf("\x1b[%dm%d\x1b[0m ", c, c) }
javascript
for (let c = 30; c <= 37; c++) process.stdout.write(`\x1b[${c}m${c}\x1b[0m `)
c
for (int c = 30; c <= 37; c++) printf("\x1b[%dm%d\x1b[0m ", c, 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