You can make your BASH script more pretty, by colorizing its output.

Use ANSI escape sequences to set text properties like foreground and background colors.

Colorizing Shell

Use the following template for writing colored text:

echo -e "\e[COLORmSample Text\e[0m"
OptionDescription
-eEnable interpretation of backslash escapes
\e[Begin the color modifications
COLORmColor Code + ‘m’ at the end
\e[0mEnd the color modifications

Examples:

$ echo -e "\e[31mRed Text\e[0m"
Red Text
$ echo -e "\e[42mGreen Background\e[0m"
Green Background

ANSI — Color Escape Codes

Shell scripts commonly use ANSI escape codes for color output:

ColorForeground CodeBackground CodeSample
Black3040
Red3141
Green3242
Brown3343
Blue3444
Purple3545
Cyan3646
Light Gray3747

Escape sequence also allows to control the manner in which characters are displayed on the screen:

ANSI CodeDescription
0Normal Characters
1Bold Characters
4Underlined Characters
5Blinking Characters
7Reverse video Characters

Examples:

$ echo -e "\e[1mBold Text\e[0m"
Bold Text
$ echo -e "\e[3mUnderlined Text\e[0m"
Underlined Text

By combining all these escape sequences, we can get more fancy effect.

echo -e "\e[COLOR1;COLOR2mSample Text\e[0m"

There are some differences between colors when combining colors with bold text attribute:

ColorForeground CodeBackground CodeSample
Dark Gray1;301;40
Light Red1;311;41
Light Green1;321;42
Yellow1;331;43
Light Blue1;341;44
Light Purple1;351;45
Light Cyan1;361;46
White1;371;47

Examples:

$ echo -e "\e[1;34mLight Blue Text\e[0m"
Light Blue Text
$ echo -e "\e[1;33;4;44mYellow Underlined Text on Blue Background\e[0m"
Yellow Underlined Text on Blue Background

Was it useful? Share this post with the world!