ANSI escape code
ANSI escape sequences are a standard for in-band signaling to control cursor location, color, font styling, and other options on video text terminals and terminal emulators. Certain sequences of bytes, most starting with an ASCII Escape and bracket character followed by parameters, are embedded into text. The terminal interprets these sequences as commands, rather than text to display verbatim.
ANSI sequences were introduced in the 1970s to replace vendor-specific sequences and became widespread in the computer equipment market by the early 1980s. They are used in development, scientific, commercial text-based applications as well as bulletin board systems to offer standardized functionality.
Although hardware text terminals have become increasingly rare in the 21st century, the relevance of the ANSI standard persists because a great majority of terminal emulators and command consoles interpret at least a portion of the ANSI standard.
How to use
이스케이프 시퀀스 덕분에 색상과 서식있는 텍스트를 표시 할 수 있습니다. 이러한 시퀀스는 이스케이프 문자와 다른 문자로 구성됩니다:
"<Esc>[
<span style="color:red">FormatCode
m"
In Bash, the
-
\e
-
\033
-
\x1B
자주 쓰는 코드
Code | Description | Example |
1 | Bold/Bright | |
2 | Dim | |
4 | Underlined | |
5 | Blink | |
7 | Reverse (invert the foreground and background colors) | |
8 | Hidden (useful for passwords) | |
CSI (Control Sequence Introducer) sequences
Select Graphic Rendition
SGR parameters
SGR (Select Graphic Rendition) sets display attributes. Several attributes can be set in the same sequence, separated by semicolons. Each display attribute remains in effect until a following occurrence of SGR resets it. If no codes are given, CSI m
is treated as CSI 0 m
(reset / normal).
In ECMA-48 SGR is called "Select Graphic Rendition". In Linux manual pages the term "Set Graphics Rendition" is used.
Code | Effect | Note |
0 | Reset / Normal | All attributes off |
1 | Bold or increased intensity | As with faint, the color change is a PC (SCO / CGA) invention. |
2 | Faint or decreased intensity | aka Dim (with a saturated color). May be implemented as a light font weight like bold. |
3 | Italic | Not widely supported. Sometimes treated as inverse or blink. |
4 | Underline | Style extensions exist for Kitty, VTE, mintty and iTerm2. |
5 | Slow Blink | less than 150 per minute |
6 | Rapid Blink | MS-DOS ANSI.SYS, 150+ per minute; not widely supported |
7 | Reverse video | swap foreground and background colors, aka invert; inconsistent emulation |
8 | Conceal | aka Hide, not widely supported. |
9 | Crossed-out | aka Strike, characters legible but marked as if for deletion. |
10 | Primary (default) font | |
11–19 | Alternative font | Select alternative font |
20 | Fraktur | Rarely supported |
21 | Doubly underline or Bold off | Double-underline per ECMA-48. |
22 | Normal color or intensity | Neither bold nor faint |
23 | Not italic, not Fraktur | |
24 | Underline off | Not singly or doubly underlined |
25 | Blink off | |
26 | Proportional spacing | ITU T.61 and T.416, not known to be used on terminals |
27 | Reverse/invert off | |
28 | Reveal | conceal off |
29 | Not crossed out | |
30–37 | Set foreground color | See color table below |
38 | Set foreground color | Next arguments are |
39 | Default foreground color | implementation defined (according to standard) |
40–47 | Set background color | See color table below |
48 | Set background color | Next arguments are |
49 | Default background color | implementation defined (according to standard) |
50 | Disable proportional spacing | T.61 and T.416 |
51 | Framed | |
52 | Encircled | Implemented as "emoji variation selector" in mintty. |
53 | Overlined | |
54 | Not framed or encircled | |
55 | Not overlined | |
58 | Set underline color | Kitty, VTE, mintty, and iTerm2. (not in standard) |
59 | Default underline color | Kitty, VTE, mintty, and iTerm2. (not in standard) |
60 | ideogram underline or right side line | Rarely supported |
61 | ideogram double underline or | |
62 | ideogram overline or left side line | |
63 | ideogram double overline or | |
64 | ideogram stress marking | |
65 | ideogram attributes off | reset the effects of all of |
73 | superscript | mintty (not in standard) |
74 | subscript | |
90–97 | Set bright foreground color | aixterm (not in standard) |
100–107 | Set bright background color |
Colors
3/4 bit
Name | FG Code | BG Code | VGA1 | Windows Console2 | Visual Studio Code | Windows 10 Console4 | Terminal.app | PuTTY | mIRC | Ubuntu5 | ||
Black | 30 | 40 | 0,0,0 | 12,12,12 | 0,0,0 | 1,1,1 | ||||||
Red | 31 | 41 | 170,0,0 | 128,0,0 | 205, 49, 49 | 197,15,31 | 194,54,33 | 187,0,0 | 127,0,0 | 205,0,0 | 222,56,43 | |
Green | 32 | 42 | 0,170,0 | 0,128,0 | 13, 188, 121 | 19,161,14 | 37,188,36 | 0,187,0 | 0,147,0 | 0,205,0 | 57,181,74 | |
Yellow | 33 | 43 | 170,85,06 | 128,128,0 | 238,237,240 | 229, 229, 16 | 193,156,0 | 173,173,39 | 187,187,0 | 252,127,0 | 205,205,0 | 255,199,6 |
Blue | 34 | 44 | 0,0,170 | 0,0,128 | 36, 114, 200 | 0,55,218 | 73,46,225 | 0,0,187 | 0,0,127 | 0,0,2387 | 0,111,184 | |
Magenta | 35 | 45 | 170,0,170 | 128,0,128 | 1,36,86 | 188, 63, 188 | 136,23,152 | 211,56,211 | 187,0,187 | 156,0,156 | 205,0,205 | 118,38,113 |
Cyan | 36 | 46 | 0,170,170 | 0,128,128 | 17, 168, 205 | 58,150,221 | 51,187,200 | 0,187,187 | 0,147,147 | 0,205,205 | 44,181,233 | |
White | 37 | 47 | 170,170,170 | 192,192,192 | 229, 229, 229 | 204,204,204 | 203,204,205 | 187,187,187 | 210,210,210 | 229,229,229 | 204,204,204 | |
Bright Black (Gray) | 90 | 100 | 85,85,85 | 128,128,128 | 102, 102, 102 | 118,118,118 | 129,131,131 | 85,85,85 | 127,127,127 | 127,127,127 | 128,128,128 | |
Bright Red | 91 | 101 | 255,85,85 | 255,0,0 | 241, 76, 76 | 231,72,86 | 252,57,31 | 255,85,85 | 255,0,0 | 255,0,0 | 255,0,0 | |
| Bright Green | 92 | 102 | 85,255,85 | 0,255,0 | 35, 209, 139 | 22,198,12 | 49,231,34 | 85,255,85 | 0,252,0 | 0,255,0 | 0,255,0 | |
| Bright Yellow | 93 | 103 | 255,255,85 | 255,255,0 | 245, 245, 67 | 249,241,165 | 234,236,35 | 255,255,85 | 255,255,0 | 255,255,0 | 255,255,0 | |
| Bright Blue | 94 | 104 | 85,85,255 | 0,0,255 | 59, 142, 234 | 59,120,255 | 88,51,255 | 85,85,255 | 0,0,252 | 92,92,2558 | 0,0,255 | |
| Bright Magenta | 95 | 105 | 255,85,255 | 255,0,255 | 214, 112, 214 | 180,0,158 | 249,53,248 | 255,85,255 | 255,0,255 | 255,0,255 | 255,0,255 | |
| Bright Cyan | 96 | 106 | 85,255,255 | 0,255,255 | 41, 184, 219 | 97,214,214 | 20,240,240 | 85,255,255 | 0,255,255 | 0,255,255 | 0,255,255 | |
| Bright White | 97 | 107 | 255,255,255 | 255,255,255 | 229, 229, 229 | 242,242,242 | 233,235,235 | 255,255,255 | 255,255,255 | 255,255,255 | 255,255,255 |
8-bit
As 256-color lookup tables became common on graphic cards, escape sequences were added to select from a pre-defined set of 256 colors
ESC[ 38;5;⟨n⟩ m Select foreground color
ESC[ 48;5;⟨n⟩ m Select background color
0- 7: standard colors (as in ESC [ 30–37 m)
8- 15: high intensity colors (as in ESC [ 90–97 m)
16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
232-255: grayscale from black to white in 24 steps
The ITU's T.416 Information technology - Open Document Architecture (ODA) and interchange format: Character content architectures uses ':' as separator characters instead:
ESC[ 38:5:⟨n⟩ m Select foreground color
ESC[ 48:5:⟨n⟩ m Select background color
256-color mode — foreground: ESC[38;5;#m background: ESC[48;5;#m | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Standard colors | High-intensity colors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
216 colors | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | ||||||||||||||||||||||||
52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | ||||||||||||||||||||||||
88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | ||||||||||||||||||||||||
124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | ||||||||||||||||||||||||
160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | ||||||||||||||||||||||||
196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | ||||||||||||||||||||||||
Grayscale colors | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
24-bit
As "true color" graphic cards with 16 to 24 bits of color became common, Xterm, KDE's Konsole, iTerm, as well as all libvte based terminals (including GNOME Terminal) support 24-bit foreground and background color setting.
ESC[ 38;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB foreground color
ESC[ 48;2;⟨r⟩;⟨g⟩;⟨b⟩ m Select RGB background color
Common Example
function make_directory
{
local dir=$1
if [[ ! -d "$dir" ]]; then
mkdir -p "$dir"
fi
}
function print_message
{
echo "$@"
}
function print_information
{
echo -e "\033[32m$@\033[0m" # Green
}
function print_warning
{
echo -e "\033[33m$@\033[0m" # Yellow
}
function print_verbose
{
if [[ $VERBOSE -ne 0 ]]; then
echo -e "\033[34m$@\033[0m" # Blue
fi
}
function print_error
{
echo -e "\033[31m$@\033[0m" 1>&2 # Red
}
function check_code_or_exit
{
local code=$?
if [[ $code -ne 0 ]]; then
print_error "An error has been detected: $code"
exit $code
fi
}
See also
Favorite site
- Wikipedia (en) ANSI escape code
- [추천] Bash tips: Colors and formatting (ANSI/VT100 Control sequences) 9
References
-
Typical colors that are used when booting PCs and leaving them in text mode, which used a 16-entry color table. The colors are different in the EGA/VGA graphic modes. ↩
-
As of Windows XP ↩
-
Until PowerShell 6 ↩
-
Campbell theme.Used as of Windows 10 1709 ↩
-
For virtual terminals, from
/etc/vtrgb
↩ -
On terminals based on CGA compatible hardware, such as ANSI.SYS running on DOS, this normal intensity foreground color is rendered as Orange. CGA RGBI monitors contained hardware to modify the dark yellow color to an orange/brown color by reducing the green component. ↩
-
Changed from 0,0,205 in July 2004 Patch #192 – 2004/7/12 – XFree86 4.4.99.9 ↩
-
Changed from 0,0,255 in July 2004 Patch #192 – 2004/7/12 – XFree86 4.4.99.9 ↩
-
Bash_tip_colors_and_formatting.pdf ↩