ANSI escape codes are sequences of characters used to control the style, color, and behavior of text output in a terminal. They are widely supported across Unix terminals, macOS Terminal, modern Windows consoles (10+), and many terminal emulators like VS Code, iTerm2, Alacritty, etc.
An ANSI escape code starts with the escape character followed by a Control Sequence Introducer (CSI), then one or more codes (parameters), and ends with a command character.
ESC [ parameters mESCis the escape character (ASCII 27 or\x1b)[is the Control Sequence Introducer (CSI)- parameters are semicolon-separated values (e.g.,
1;31) mindicates text formatting (SGR - Select Graphic Rendition)
This is used for text formatting. For example:
echo -e "\033[1;32mBold Green Text\033[0m"Outputs Hello in red, then resets.
All ANSI sequences begin with the escape character (byte 27 in decimal).
| Representation | Description |
|---|---|
\x1b |
Hex notation (common in JS) |
\033 |
Octal notation (common in Bash) |
\u001b |
Unicode notation (JSON-safe) |
\e |
Shorthand in Bash/Zsh |
SGR parameters define text style, color, and effects.
| Code | Style |
|---|---|
| 0 | Reset all |
| 1 | Bold |
| 2 | Dim |
| 3 | Italic |
| 4 | Underline |
| 5 | Blink (slow) |
| 6 | Blink (fast) |
| 7 | Reverse (invert fg/bg) |
| 8 | Hidden |
| 9 | Strikethrough |
Reset Variants:
| Code | Resets |
|---|---|
| 21 | Bold/Double Underline |
| 22 | Bold + Dim |
| 23 | Italic |
| 24 | Underline |
| 25 | Blink |
| 27 | Inverse |
| 28 | Conceal |
| 29 | Strikethrough |
| Code | Color | Bright Code | Bright Color |
|---|---|---|---|
| 30 | Black | 90 | Bright Black (Gray) |
| 31 | Red | 91 | Bright Red |
| 32 | Green | 92 | Bright Green |
| 33 | Yellow | 93 | Bright Yellow |
| 34 | Blue | 94 | Bright Blue |
| 35 | Magenta | 95 | Bright Magenta |
| 36 | Cyan | 96 | Bright Cyan |
| 37 | White | 97 | Bright White |
| Code | Color | Bright Code | Bright Color |
|---|---|---|---|
| 40 | Black | 100 | Bright Black |
| 41 | Red | 101 | Bright Red |
| 42 | Green | 102 | Bright Green |
| 43 | Yellow | 103 | Bright Yellow |
| 44 | Blue | 104 | Bright Blue |
| 45 | Magenta | 105 | Bright Magenta |
| 46 | Cyan | 106 | Bright Cyan |
| 47 | White | 107 | Bright White |
Format:
- Foreground:
\033[38;5;<n>m - Background:
\033[48;5;<n>m
Where <n> is from 0 to 255.
| Range | Meaning |
|---|---|
| 0–15 | Standard + Bright colors |
| 16–231 | 6×6×6 RGB cube |
| 232–255 | Grayscale ramp (24 steps) |
Each RGB channel (r, g, b) can be from 0–5 (6 levels).
Formula:
code = 16 + (36 × r) + (6 × g) + bExample:
echo -e "\033[38;5;196mBright Red\033[0m"24 shades from near black to white, no color hue.
for i in {232..255}; do
echo -ne "\033[48;5;${i}m \033[0m"
doneFormat:
- Foreground:
\033[38;2;<r>;<g>;<b>m - Background:
\033[48;2;<r>;<g>;<b>m
Where r, g, and b are 0–255.
Examples
echo -e "\033[38;2;255;140;0mOrange\033[0m"
echo -e "\033[48;2;0;0;64;38;2;255;255;255m White on Navy \033[0m"Gradient Example
for i in {0..255..25}; do
echo -ne "\033[48;2;${i};0;255m ${i} \033[0m"
doneYou can combine multiple SGR parameters with semicolons:
ESC="\033"
echo -e "${ESC}[1;4;31mBold Underlined Red${ESC}[0m"ANSI codes are supported on:
-
Unix (Linux, macOS)
-
Windows 10+ (older versions require ANSICON or enabling VT)
-
Node.js (supports ANSI natively)
-
Browsers (via dev tools console with limited support)
-
Many programming languages:
- Python:
print("\033[31mRed\033[0m") - Bash:
echo -e "\033[32mGreen\033[0m" - JavaScript:
console.log("\x1b[36mCyan\x1b[0m")
- Python:
Always end ANSI sequences with reset to avoid carryover:
RESET = "\x1b[0m";const style = (...codes: number[]) => `\x1b[${codes.join(";")}m`;
console.log(`${style(1, 34)}Bold Blue${style(0)}`);color() { echo -e "\033[$1m$2\033[0m"; }
color "1;33" "Bold Yellow Text"| Code | Function |
|---|---|
\x1b[H |
Move to home position |
\x1b[2J |
Clear screen |
\x1b[K |
Clear line to right |
\x1b[<n>A |
Move cursor up |
\x1b[<n>B |
Move cursor down |
\x1b[<n>C |
Move cursor forward |
\x1b[<n>D |
Move cursor backward |