Definitive Guide For Colored Text In Terminal

NOTE: Content of this page is being moved to this page and this one.

Text with colors in terminal makes your terminal output easier to read. In this post, we will help you understand how colored text works in terminal, and also share the ansi color codings we used to print colored text.

How Colored Text Works In Terminal?

Terminal applications (e.g., bash, vim, emacs), uses escape sequences (or escape codes) to control the behavior of the terminal. Setting the text color is one of the bahaviors that can be controlled by escape codes.

More specifically, each terminal maintains the current text coloring settings (one for foreground, one for background). Terminal applications can send ansi color coding escape codes to set the terminal’s current text coloring settings. After that, any text printed on terminal will be presented in that color.

If a terminal application wants to print colored text (for example, red), it can take the following three steps:

  1. Send escape codes to set the current terminal foreground color to red.
  2. Send the text to the terminal.
  3. Send escape codes to reset the text color settings for terminal.

(If you’d like to understand more about how terminal works, check out this post, or this udemy course)

Ansi Color Coding (Or Ansi Color Codes)

In the previous section, we mentioned that we use ansi color coding escape codes for colored text. In this section, we’ll list them all.

8 Color Escape Codes

8 color is supported by all terminal by default. Their escape codes are listed in the following chart.

Name Color RGB str Foreground Color Code Background Color Code
Black #000000 \033[1;30m \033[1;40m
Red #cc0000 \033[1;31m \033[1;41m
Green #4e9a06 \033[1;32m \033[1;42m
Yellow #c4a000 \033[1;33m \033[1;43m
Blue #729fcf \033[1;34m \033[1;44m
Magenta #75507b \033[1;35m \033[1;45m
Cyan #06989a \033[1;36m \033[1;46m
White #d3d7cf \033[1;37m \033[1;47m
Default \033[1;39m \033[1;49m

You can use the following command (in bash) for testing:

echo -e "\033[1;32mtext\033[0m"

256 Color Escape Codes

256 colors are supported by almost all the terminals. To enable 256 colors, make sure you have your environment variable TERM set as xterm-256colors.

The terminal 256 color uses the following escape codes:

  • foreground color: \033[38:5:${number}m
  • background color: \033[48:5:${number}m

The following chart lists the default 256 colors supported in the terminal.

Number Name Color RGB str Foreground Escape Code Background Escape Code
0 Black (SYSTEM) #000000 \033[38:5:0m \033[48:5:0m
1 Maroon (SYSTEM) #800000 \033[38:5:1m \033[48:5:1m
2 Green (SYSTEM) #008000 \033[38:5:2m \033[48:5:2m
3 Olive (SYSTEM) #808000 \033[38:5:3m \033[48:5:3m
4 Navy (SYSTEM) #000080 \033[38:5:4m \033[48:5:4m
5 Purple (SYSTEM) #800080 \033[38:5:5m \033[48:5:5m
6 Teal (SYSTEM) #008080 \033[38:5:6m \033[48:5:6m
7 Silver (SYSTEM) #c0c0c0 \033[38:5:7m \033[48:5:7m
8 Grey (SYSTEM) #808080 \033[38:5:8m \033[48:5:8m
9 Red (SYSTEM) #ff0000 \033[38:5:9m \033[48:5:9m
10 Lime (SYSTEM) #00ff00 \033[38:5:10m \033[48:5:10m
11 Yellow (SYSTEM) #ffff00 \033[38:5:11m \033[48:5:11m
12 Blue (SYSTEM) #0000ff \033[38:5:12m \033[48:5:12m
13 Fuchsia (SYSTEM) #ff00ff \033[38:5:13m \033[48:5:13m
14 Aqua (SYSTEM) #00ffff \033[38:5:14m \033[48:5:14m
15 White (SYSTEM) #ffffff \033[38:5:15m \033[48:5:15m
16 Grey0 #000000 \033[38:5:16m \033[48:5:16m
17 NavyBlue #00005f \033[38:5:17m \033[48:5:17m
18 DarkBlue #000087 \033[38:5:18m \033[48:5:18m
19 Blue3 #0000af \033[38:5:19m \033[48:5:19m
20 Blue3 #0000d7 \033[38:5:20m \033[48:5:20m
21 Blue1 #0000ff \033[38:5:21m \033[48:5:21m
22 DarkGreen #005f00 \033[38:5:22m \033[48:5:22m
23 DeepSkyBlue4 #005f5f \033[38:5:23m \033[48:5:23m
24 DeepSkyBlue4 #005f87 \033[38:5:24m \033[48:5:24m
25 DeepSkyBlue4 #005faf \033[38:5:25m \033[48:5:25m
26 DodgerBlue3 #005fd7 \033[38:5:26m \033[48:5:26m
27 DodgerBlue2 #005fff \033[38:5:27m \033[48:5:27m
28 Green4 #008700 \033[38:5:28m \033[48:5:28m
29 SpringGreen4 #00875f \033[38:5:29m \033[48:5:29m
30 Turquoise4 #008787 \033[38:5:30m \033[48:5:30m
31 DeepSkyBlue3 #0087af \033[38:5:31m \033[48:5:31m
32 DeepSkyBlue3 #0087d7 \033[38:5:32m \033[48:5:32m
33 DodgerBlue1 #0087ff \033[38:5:33m \033[48:5:33m
34 Green3 #00af00 \033[38:5:34m \033[48:5:34m
35 SpringGreen3 #00af5f \033[38:5:35m \033[48:5:35m
36 DarkCyan #00af87 \033[38:5:36m \033[48:5:36m
37 LightSeaGreen #00afaf \033[38:5:37m \033[48:5:37m
38 DeepSkyBlue2 #00afd7 \033[38:5:38m \033[48:5:38m
39 DeepSkyBlue1 #00afff \033[38:5:39m \033[48:5:39m
40 Green3 #00d700 \033[38:5:40m \033[48:5:40m
41 SpringGreen3 #00d75f \033[38:5:41m \033[48:5:41m
42 SpringGreen2 #00d787 \033[38:5:42m \033[48:5:42m
43 Cyan3 #00d7af \033[38:5:43m \033[48:5:43m
44 DarkTurquoise #00d7d7 \033[38:5:44m \033[48:5:44m
45 Turquoise2 #00d7ff \033[38:5:45m \033[48:5:45m
46 Green1 #00ff00 \033[38:5:46m \033[48:5:46m
47 SpringGreen2 #00ff5f \033[38:5:47m \033[48:5:47m
48 SpringGreen1 #00ff87 \033[38:5:48m \033[48:5:48m
49 MediumSpringGreen #00ffaf \033[38:5:49m \033[48:5:49m
50 Cyan2 #00ffd7 \033[38:5:50m \033[48:5:50m
51 Cyan1 #00ffff \033[38:5:51m \033[48:5:51m
52 DarkRed #5f0000 \033[38:5:52m \033[48:5:52m
53 DeepPink4 #5f005f \033[38:5:53m \033[48:5:53m
54 Purple4 #5f0087 \033[38:5:54m \033[48:5:54m
55 Purple4 #5f00af \033[38:5:55m \033[48:5:55m
56 Purple3 #5f00d7 \033[38:5:56m \033[48:5:56m
57 BlueViolet #5f00ff \033[38:5:57m \033[48:5:57m
58 Orange4 #5f5f00 \033[38:5:58m \033[48:5:58m
59 Grey37 #5f5f5f \033[38:5:59m \033[48:5:59m
60 MediumPurple4 #5f5f87 \033[38:5:60m \033[48:5:60m
61 SlateBlue3 #5f5faf \033[38:5:61m \033[48:5:61m
62 SlateBlue3 #5f5fd7 \033[38:5:62m \033[48:5:62m
63 RoyalBlue1 #5f5fff \033[38:5:63m \033[48:5:63m
64 Chartreuse4 #5f8700 \033[38:5:64m \033[48:5:64m
65 DarkSeaGreen4 #5f875f \033[38:5:65m \033[48:5:65m
66 PaleTurquoise4 #5f8787 \033[38:5:66m \033[48:5:66m
67 SteelBlue #5f87af \033[38:5:67m \033[48:5:67m
68 SteelBlue3 #5f87d7 \033[38:5:68m \033[48:5:68m
69 CornflowerBlue #5f87ff \033[38:5:69m \033[48:5:69m
70 Chartreuse3 #5faf00 \033[38:5:70m \033[48:5:70m
71 DarkSeaGreen4 #5faf5f \033[38:5:71m \033[48:5:71m
72 CadetBlue #5faf87 \033[38:5:72m \033[48:5:72m
73 CadetBlue #5fafaf \033[38:5:73m \033[48:5:73m
74 SkyBlue3 #5fafd7 \033[38:5:74m \033[48:5:74m
75 SteelBlue1 #5fafff \033[38:5:75m \033[48:5:75m
76 Chartreuse3 #5fd700 \033[38:5:76m \033[48:5:76m
77 PaleGreen3 #5fd75f \033[38:5:77m \033[48:5:77m
78 SeaGreen3 #5fd787 \033[38:5:78m \033[48:5:78m
79 Aquamarine3 #5fd7af \033[38:5:79m \033[48:5:79m
80 MediumTurquoise #5fd7d7 \033[38:5:80m \033[48:5:80m
81 SteelBlue1 #5fd7ff \033[38:5:81m \033[48:5:81m
82 Chartreuse2 #5fff00 \033[38:5:82m \033[48:5:82m
83 SeaGreen2 #5fff5f \033[38:5:83m \033[48:5:83m
84 SeaGreen1 #5fff87 \033[38:5:84m \033[48:5:84m
85 SeaGreen1 #5fffaf \033[38:5:85m \033[48:5:85m
86 Aquamarine1 #5fffd7 \033[38:5:86m \033[48:5:86m
87 DarkSlateGray2 #5fffff \033[38:5:87m \033[48:5:87m
88 DarkRed #870000 \033[38:5:88m \033[48:5:88m
89 DeepPink4 #87005f \033[38:5:89m \033[48:5:89m
90 DarkMagenta #870087 \033[38:5:90m \033[48:5:90m
91 DarkMagenta #8700af \033[38:5:91m \033[48:5:91m
92 DarkViolet #8700d7 \033[38:5:92m \033[48:5:92m
93 Purple #8700ff \033[38:5:93m \033[48:5:93m
94 Orange4 #875f00 \033[38:5:94m \033[48:5:94m
95 LightPink4 #875f5f \033[38:5:95m \033[48:5:95m
96 Plum4 #875f87 \033[38:5:96m \033[48:5:96m
97 MediumPurple3 #875faf \033[38:5:97m \033[48:5:97m
98 MediumPurple3 #875fd7 \033[38:5:98m \033[48:5:98m
99 SlateBlue1 #875fff \033[38:5:99m \033[48:5:99m
100 Yellow4 #878700 \033[38:5:100m \033[48:5:100m
101 Wheat4 #87875f \033[38:5:101m \033[48:5:101m
102 Grey53 #878787 \033[38:5:102m \033[48:5:102m
103 LightSlateGrey #8787af \033[38:5:103m \033[48:5:103m
104 MediumPurple #8787d7 \033[38:5:104m \033[48:5:104m
105 LightSlateBlue #8787ff \033[38:5:105m \033[48:5:105m
106 Yellow4 #87af00 \033[38:5:106m \033[48:5:106m
107 DarkOliveGreen3 #87af5f \033[38:5:107m \033[48:5:107m
108 DarkSeaGreen #87af87 \033[38:5:108m \033[48:5:108m
109 LightSkyBlue3 #87afaf \033[38:5:109m \033[48:5:109m
110 LightSkyBlue3 #87afd7 \033[38:5:110m \033[48:5:110m
111 SkyBlue2 #87afff \033[38:5:111m \033[48:5:111m
112 Chartreuse2 #87d700 \033[38:5:112m \033[48:5:112m
113 DarkOliveGreen3 #87d75f \033[38:5:113m \033[48:5:113m
114 PaleGreen3 #87d787 \033[38:5:114m \033[48:5:114m
115 DarkSeaGreen3 #87d7af \033[38:5:115m \033[48:5:115m
116 DarkSlateGray3 #87d7d7 \033[38:5:116m \033[48:5:116m
117 SkyBlue1 #87d7ff \033[38:5:117m \033[48:5:117m
118 Chartreuse1 #87ff00 \033[38:5:118m \033[48:5:118m
119 LightGreen #87ff5f \033[38:5:119m \033[48:5:119m
120 LightGreen #87ff87 \033[38:5:120m \033[48:5:120m
121 PaleGreen1 #87ffaf \033[38:5:121m \033[48:5:121m
122 Aquamarine1 #87ffd7 \033[38:5:122m \033[48:5:122m
123 DarkSlateGray1 #87ffff \033[38:5:123m \033[48:5:123m
124 Red3 #af0000 \033[38:5:124m \033[48:5:124m
125 DeepPink4 #af005f \033[38:5:125m \033[48:5:125m
126 MediumVioletRed #af0087 \033[38:5:126m \033[48:5:126m
127 Magenta3 #af00af \033[38:5:127m \033[48:5:127m
128 DarkViolet #af00d7 \033[38:5:128m \033[48:5:128m
129 Purple #af00ff \033[38:5:129m \033[48:5:129m
130 DarkOrange3 #af5f00 \033[38:5:130m \033[48:5:130m
131 IndianRed #af5f5f \033[38:5:131m \033[48:5:131m
132 HotPink3 #af5f87 \033[38:5:132m \033[48:5:132m
133 MediumOrchid3 #af5faf \033[38:5:133m \033[48:5:133m
134 MediumOrchid #af5fd7 \033[38:5:134m \033[48:5:134m
135 MediumPurple2 #af5fff \033[38:5:135m \033[48:5:135m
136 DarkGoldenrod #af8700 \033[38:5:136m \033[48:5:136m
137 LightSalmon3 #af875f \033[38:5:137m \033[48:5:137m
138 RosyBrown #af8787 \033[38:5:138m \033[48:5:138m
139 Grey63 #af87af \033[38:5:139m \033[48:5:139m
140 MediumPurple2 #af87d7 \033[38:5:140m \033[48:5:140m
141 MediumPurple1 #af87ff \033[38:5:141m \033[48:5:141m
142 Gold3 #afaf00 \033[38:5:142m \033[48:5:142m
143 DarkKhaki #afaf5f \033[38:5:143m \033[48:5:143m
144 NavajoWhite3 #afaf87 \033[38:5:144m \033[48:5:144m
145 Grey69 #afafaf \033[38:5:145m \033[48:5:145m
146 LightSteelBlue3 #afafd7 \033[38:5:146m \033[48:5:146m
147 LightSteelBlue #afafff \033[38:5:147m \033[48:5:147m
148 Yellow3 #afd700 \033[38:5:148m \033[48:5:148m
149 DarkOliveGreen3 #afd75f \033[38:5:149m \033[48:5:149m
150 DarkSeaGreen3 #afd787 \033[38:5:150m \033[48:5:150m
151 DarkSeaGreen2 #afd7af \033[38:5:151m \033[48:5:151m
152 LightCyan3 #afd7d7 \033[38:5:152m \033[48:5:152m
153 LightSkyBlue1 #afd7ff \033[38:5:153m \033[48:5:153m
154 GreenYellow #afff00 \033[38:5:154m \033[48:5:154m
155 DarkOliveGreen2 #afff5f \033[38:5:155m \033[48:5:155m
156 PaleGreen1 #afff87 \033[38:5:156m \033[48:5:156m
157 DarkSeaGreen2 #afffaf \033[38:5:157m \033[48:5:157m
158 DarkSeaGreen1 #afffd7 \033[38:5:158m \033[48:5:158m
159 PaleTurquoise1 #afffff \033[38:5:159m \033[48:5:159m
160 Red3 #d70000 \033[38:5:160m \033[48:5:160m
161 DeepPink3 #d7005f \033[38:5:161m \033[48:5:161m
162 DeepPink3 #d70087 \033[38:5:162m \033[48:5:162m
163 Magenta3 #d700af \033[38:5:163m \033[48:5:163m
164 Magenta3 #d700d7 \033[38:5:164m \033[48:5:164m
165 Magenta2 #d700ff \033[38:5:165m \033[48:5:165m
166 DarkOrange3 #d75f00 \033[38:5:166m \033[48:5:166m
167 IndianRed #d75f5f \033[38:5:167m \033[48:5:167m
168 HotPink3 #d75f87 \033[38:5:168m \033[48:5:168m
169 HotPink2 #d75faf \033[38:5:169m \033[48:5:169m
170 Orchid #d75fd7 \033[38:5:170m \033[48:5:170m
171 MediumOrchid1 #d75fff \033[38:5:171m \033[48:5:171m
172 Orange3 #d78700 \033[38:5:172m \033[48:5:172m
173 LightSalmon3 #d7875f \033[38:5:173m \033[48:5:173m
174 LightPink3 #d78787 \033[38:5:174m \033[48:5:174m
175 Pink3 #d787af \033[38:5:175m \033[48:5:175m
176 Plum3 #d787d7 \033[38:5:176m \033[48:5:176m
177 Violet #d787ff \033[38:5:177m \033[48:5:177m
178 Gold3 #d7af00 \033[38:5:178m \033[48:5:178m
179 LightGoldenrod3 #d7af5f \033[38:5:179m \033[48:5:179m
180 Tan #d7af87 \033[38:5:180m \033[48:5:180m
181 MistyRose3 #d7afaf \033[38:5:181m \033[48:5:181m
182 Thistle3 #d7afd7 \033[38:5:182m \033[48:5:182m
183 Plum2 #d7afff \033[38:5:183m \033[48:5:183m
184 Yellow3 #d7d700 \033[38:5:184m \033[48:5:184m
185 Khaki3 #d7d75f \033[38:5:185m \033[48:5:185m
186 LightGoldenrod2 #d7d787 \033[38:5:186m \033[48:5:186m
187 LightYellow3 #d7d7af \033[38:5:187m \033[48:5:187m
188 Grey84 #d7d7d7 \033[38:5:188m \033[48:5:188m
189 LightSteelBlue1 #d7d7ff \033[38:5:189m \033[48:5:189m
190 Yellow2 #d7ff00 \033[38:5:190m \033[48:5:190m
191 DarkOliveGreen1 #d7ff5f \033[38:5:191m \033[48:5:191m
192 DarkOliveGreen1 #d7ff87 \033[38:5:192m \033[48:5:192m
193 DarkSeaGreen1 #d7ffaf \033[38:5:193m \033[48:5:193m
194 Honeydew2 #d7ffd7 \033[38:5:194m \033[48:5:194m
195 LightCyan1 #d7ffff \033[38:5:195m \033[48:5:195m
196 Red1 #ff0000 \033[38:5:196m \033[48:5:196m
197 DeepPink2 #ff005f \033[38:5:197m \033[48:5:197m
198 DeepPink1 #ff0087 \033[38:5:198m \033[48:5:198m
199 DeepPink1 #ff00af \033[38:5:199m \033[48:5:199m
200 Magenta2 #ff00d7 \033[38:5:200m \033[48:5:200m
201 Magenta1 #ff00ff \033[38:5:201m \033[48:5:201m
202 OrangeRed1 #ff5f00 \033[38:5:202m \033[48:5:202m
203 IndianRed1 #ff5f5f \033[38:5:203m \033[48:5:203m
204 IndianRed1 #ff5f87 \033[38:5:204m \033[48:5:204m
205 HotPink #ff5faf \033[38:5:205m \033[48:5:205m
206 HotPink #ff5fd7 \033[38:5:206m \033[48:5:206m
207 MediumOrchid1 #ff5fff \033[38:5:207m \033[48:5:207m
208 DarkOrange #ff8700 \033[38:5:208m \033[48:5:208m
209 Salmon1 #ff875f \033[38:5:209m \033[48:5:209m
210 LightCoral #ff8787 \033[38:5:210m \033[48:5:210m
211 PaleVioletRed1 #ff87af \033[38:5:211m \033[48:5:211m
212 Orchid2 #ff87d7 \033[38:5:212m \033[48:5:212m
213 Orchid1 #ff87ff \033[38:5:213m \033[48:5:213m
214 Orange1 #ffaf00 \033[38:5:214m \033[48:5:214m
215 SandyBrown #ffaf5f \033[38:5:215m \033[48:5:215m
216 LightSalmon1 #ffaf87 \033[38:5:216m \033[48:5:216m
217 LightPink1 #ffafaf \033[38:5:217m \033[48:5:217m
218 Pink1 #ffafd7 \033[38:5:218m \033[48:5:218m
219 Plum1 #ffafff \033[38:5:219m \033[48:5:219m
220 Gold1 #ffd700 \033[38:5:220m \033[48:5:220m
221 LightGoldenrod2 #ffd75f \033[38:5:221m \033[48:5:221m
222 LightGoldenrod2 #ffd787 \033[38:5:222m \033[48:5:222m
223 NavajoWhite1 #ffd7af \033[38:5:223m \033[48:5:223m
224 MistyRose1 #ffd7d7 \033[38:5:224m \033[48:5:224m
225 Thistle1 #ffd7ff \033[38:5:225m \033[48:5:225m
226 Yellow1 #ffff00 \033[38:5:226m \033[48:5:226m
227 LightGoldenrod1 #ffff5f \033[38:5:227m \033[48:5:227m
228 Khaki1 #ffff87 \033[38:5:228m \033[48:5:228m
229 Wheat1 #ffffaf \033[38:5:229m \033[48:5:229m
230 Cornsilk1 #ffffd7 \033[38:5:230m \033[48:5:230m
231 Grey100 #ffffff \033[38:5:231m \033[48:5:231m
232 Grey3 #080808 \033[38:5:232m \033[48:5:232m
233 Grey7 #121212 \033[38:5:233m \033[48:5:233m
234 Grey11 #1c1c1c \033[38:5:234m \033[48:5:234m
235 Grey15 #262626 \033[38:5:235m \033[48:5:235m
236 Grey19 #303030 \033[38:5:236m \033[48:5:236m
237 Grey23 #3a3a3a \033[38:5:237m \033[48:5:237m
238 Grey27 #444444 \033[38:5:238m \033[48:5:238m
239 Grey30 #4e4e4e \033[38:5:239m \033[48:5:239m
240 Grey35 #585858 \033[38:5:240m \033[48:5:240m
241 Grey39 #626262 \033[38:5:241m \033[48:5:241m
242 Grey42 #6c6c6c \033[38:5:242m \033[48:5:242m
243 Grey46 #767676 \033[38:5:243m \033[48:5:243m
244 Grey50 #808080 \033[38:5:244m \033[48:5:244m
245 Grey54 #8a8a8a \033[38:5:245m \033[48:5:245m
246 Grey58 #949494 \033[38:5:246m \033[48:5:246m
247 Grey62 #9e9e9e \033[38:5:247m \033[48:5:247m
248 Grey66 #a8a8a8 \033[38:5:248m \033[48:5:248m
249 Grey70 #b2b2b2 \033[38:5:249m \033[48:5:249m
250 Grey74 #bcbcbc \033[38:5:250m \033[48:5:250m
251 Grey78 #c6c6c6 \033[38:5:251m \033[48:5:251m
252 Grey82 #d0d0d0 \033[38:5:252m \033[48:5:252m
253 Grey85 #dadada \033[38:5:253m \033[48:5:253m
254 Grey89 #e4e4e4 \033[38:5:254m \033[48:5:254m
255 Grey93 #eeeeee \033[38:5:255m \033[48:5:255m

You can use the following command (in bash) for testing:

echo -e "\033[38:5:1mtext\033[0m"

True Color Escape Codes

Terminal true color uses the following escape codes:

  • foreground color: \033[38;2;${red};${green};${blue}m
  • background color: \033[48;2;${red};${green};${blue}m

You can use the following command (in bash) for testing:

echo -e "\033[38;2;255;0;0mtext\033[0m"

Note that true color is not supported by all terminals. Only a few supports it, e.g., iterm2 on mac, mintty on Windows, xterm on Linux.

Color Reset

To reset the color settings to default, use \033[0m.