Fundamentals 6 min read

Master Linux Shell Text and Background Colors with ANSI Escape Codes

This guide explains how to use ANSI escape sequences in Linux shell scripts to change text color, background color, apply effects like bold or blinking, and provides ready‑to‑copy command examples for various color combinations.

ITPUB
ITPUB
ITPUB
Master Linux Shell Text and Background Colors with ANSI Escape Codes

This article explains how to adjust font colors in Linux terminals, a technique frequently used in shell scripts. It introduces ANSI escape sequences, shows a basic example, lists the numeric codes for different attributes, and provides ready‑to‑run command snippets for foreground colors, background colors, and special effects such as blinking.

Basic Example

The following command sets a blue background, white foreground, enables blinking, prints the word “ME”, resets the terminal attributes, and then prints “COOL”. The -e option enables interpretation of backslash‑escaped characters. echo -e "\033[44;37;5m ME \033[0m COOL" In the escape sequence, \033 starts the control code, m ends it, and the numbers before m specify the attributes. Here 44;37;5 means blue background (44), white foreground (37), and blinking (5). Changing these numbers produces different color combinations; the order of the numbers does not matter.

ANSI Attribute Codes

Below is a list of the most common codes. Combine them with semicolons to achieve the desired effect.

0   Reset all attributes to default
1   Bold
2   Dim (half‑bright)
4   Underline
5   Blink
7   Reverse video
22  Normal intensity
24  No underline
25  No blink
27  No reverse video
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 foreground (default color)
39  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

Foreground Color Examples

echo -e "\033[31m 红色字 \033[0m"

– red text echo -e "\033[34m 黄色字 \033[0m" – yellow text echo -e "\033[41;33m 红底黄字 \033[0m" – red background, yellow text echo -e "\033[41;37m 红底白字 \033[0m" – red background, white text

Complete Foreground Color List

echo -e "\033[30m 黑色字 \033[0m"
echo -e "\033[31m 红色字 \033[0m"
echo -e "\033[32m 绿色字 \033[0m"
echo -e "\033[33m 黄色字 \033[0m"
echo -e "\033[34m 蓝色字 \033[0m"
echo -e "\033[35m 紫色字 \033[0m"
echo -e "\033[36m 天蓝字 \033[0m"
echo -e "\033[37m 白色字 \033[0m"

Background Color Examples (40–47)

echo -e "\033[40;37m 黑底白字 \033[0m"
echo -e "\033[41;37m 红底白字 \033[0m"
echo -e "\033[42;37m 绿底白字 \033[0m"
echo -e "\033[43;37m 黄底白字 \033[0m"
echo -e "\033[44;37m 蓝底白字 \033[0m"
echo -e "\033[45;37m 紫底白字 \033[0m"
echo -e "\033[46;37m 天蓝底白字 \033[0m"
echo -e "\033[47;30m 白底黑字 \033[0m"

Blinking Text

echo -e "\033[42;30;5m wwww \033[0m"
echo -e "\033[47;30;5m wwww \033[0m"

All the color and effect codes listed above can be combined as needed to style terminal output in shell scripts.

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.

ShellScriptingTerminal 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.