Make Your Shell Scripts Pop: Master ANSI Color Codes for Stunning Terminal Output
Learn how to enhance your shell script output with vibrant ANSI color codes, using the echo -e command to set foreground, background, and text attributes, and explore a comprehensive table of color codes and practical examples for creating eye‑catching terminal displays.
Shell scripts are often limited to plain black‑and‑white text, but you can make the output visually striking by using ANSI escape sequences with echo -e to control colors, background, and text effects. echo -e "\033[44;37;5m So Cool! \033[0m" This command prints the phrase "So Cool!" with a blue background, white foreground, and a blinking cursor. The -e option enables interpretation of backslash‑escaped characters, \033 starts an escape sequence, and m terminates it. The numbers 44;37;5 set background blue (44), foreground white (37), and blinking (5). Changing these numbers produces different color combinations.
Color Code Meanings
0 Reset all attributes to default
1 Bold
2 Dim (half‑bright)
4 Underline
5 Blink
7 Reverse video
22 Normal intensity
24 Underline off
25 Blink off
27 Reverse video off
30 Black foreground
31 Red foreground
32 Green foreground
33 Yellow foreground
34 Blue foreground
35 Magenta foreground
36 Cyan foreground
37 White foreground
38 Underline on default foreground
39 Underline off default foreground
40 Black background
41 Red background
42 Green background
43 Yellow background
44 Blue background
45 Magenta background
46 Cyan background
47 White background
49 Default backgroundBy mixing foreground (30‑37) and background (40‑47) codes, you can create a wide range of text styles.
Combining Color Codes
echo -e "\033[31m Red text \033[0m"
echo -e "\033[34m Yellow text \033[0m"
echo -e "\033[41;33m Red background, Yellow text \033[0m"
echo -e "\033[41;37m Red background, White text \033[0m"Foreground Color Examples (30‑37)
echo -e "\033[30m Black text \033[0m"
echo -e "\033[31m Red text \033[0m"
echo -e "\033[32m Green text \033[0m"
echo -e "\033[33m Yellow text \033[0m"
echo -e "\033[34m Blue text \033[0m"
echo -e "\033[35m Magenta text \033[0m"
echo -e "\033[36m Cyan text \033[0m"
echo -e "\033[37m White text \033[0m"Background Color Examples (40‑47)
echo -e "\033[40;37m Black background, White text \033[0m"
echo -e "\033[41;37m Red background, White text \033[0m"
echo -e "\033[42;37m Green background, White text \033[0m"
echo -e "\033[43;37m Yellow background, White text \033[0m"
echo -e "\033[44;37m Blue background, White text \033[0m"
echo -e "\033[45;37m Magenta background, White text \033[0m"
echo -e "\033[46;37m Cyan background, White text \033[0m"
echo -e "\033[47;30m White background, Black text \033[0m"By adjusting the numeric codes, you can create countless color combinations to make your shell script output more readable and visually appealing.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
