Fundamentals 6 min read

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.

ITPUB
ITPUB
ITPUB
Make Your Shell Scripts Pop: Master ANSI Color Codes for Stunning Terminal Output

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 background

By 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"
Example of colored shell output
Example of colored shell output

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"
Foreground color samples
Foreground color samples

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"
Background color samples
Background color samples

By adjusting the numeric codes, you can create countless color combinations to make your shell script output more readable and visually appealing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxBashANSIterminal-colors
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.