Post

Colorize Terminal Text with Escape Sequences in Linux

Escape sequence in Linux

ANSI escape codes can be used to style the output of echo or printf. An escape code is a sequence of characters start with an ESC(033 in Octal representation) character, followed by second character in the ASCII range 64 to 95. This sequence can be of different length.

The general format of escape code for styling the terminal text is -

ESC [ {attr1};{attr2};....{attrn)m.

The ASCII equivalent of ESC character is 033(in Octal) or 0x1b(in Hexadecimal). We use 033 because it works for all operating systems.

Some examples for escape codes are -

1
2
3
4
'\033[0m'       #Reset text
'\033[0;33m'    #Yello color text
'\033[42m'      #Green background
'\033[1;42m'    #Bold text with Green background

Note that above escape code sequeces ends with m and there is no ; before that.

If we examine the code ‘\033[1;42m’,

\033[1;42m
Octal value of ESC char.left square bracketAttribute 1 (bold)DividerAttribute 2 (Green)Ending Char

Usage with echo

1
echo -e '\033[1;42m'This is text with green background

Don’t forget to use -e option along with echo command. Any text following the above code sequence will print with green background color.

Usage with printf

1
printf '\033[1;42m'This is text with green background

Text formatting

ValueEscape CodeText Style
0‘\033[0;31m’Regular
1‘\033[1;31m’Bold
2‘\033[2;31m’Low Intensity
3‘\033[3;31m’Italic
4‘\033[4;31m’Underline
5‘\033[5;31m’Blinking
6‘\033[6;31m’Reverse
7‘\033[7;31m’Background
8‘\033[8;31m’Invisible

Terminal text decoration with escape codes Terminal text decoration with escape codes

Text Color

ValueEscape CodeText Color
30‘\033[0;30m’Black
31‘\033[0;31m’Red
32‘\033[0;32m’Green
33‘\033[0;33m’Yello
34‘\033[0;34m’Blue
35‘\033[0;35m’Magenta
36‘\033[0;36m’Cyan
37‘\033[0;37m’White

Terminal text color with escape codes Terminal text color with escape codes

Background color

ValueEscape CodeBackground Color
40‘\033[0;40m’Black
41‘\033[0;41m’Red
42‘\033[0;42m’Green
43‘\033[0;43m’Yello
44‘\033[0;44m’Blue
45‘\033[0;45m’Magenta
46‘\033[0;46m’Cyan
47‘\033[0;47m’White

Terminal text with Background Color Teminal text with background color

Random text stype attributes

One or more attributes can be clubbed together for more styling.

Terminal text with Random Text attributes Terminal text with Random Text attributes

A simple color echo solution for shell scripts

Save the following code in a file, let’s say cecho.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cecho() {
	local code="\033["
	case "$1" in
		black  | bk) color="${code}0;30m";;
		red    |  r) color="${code}1;31m";;
		green  |  g) color="${code}1;32m";;
		yellow |  y) color="${code}1;33m";;
		blue   |  b) color="${code}1;34m";;
		purple |  p) color="${code}1;35m";;
		cyan   |  c) color="${code}1;36m";;
		gray   | gr) color="${code}0;37m";;
		*) local text="$1"
			esac
			[ -z "$text" ] && local text="$color$2${code}0m"
			echo -e "$text"
}

Including the above file in any shell script will allow us to print color text on terminal.

Let’s say file display.sh has the following content.

1
2
3
4
#!/bin/sh
. /path/to/cecho.sh

cecho red "This is red text"

Now executing second shell script will display output in red color.

1
bash display.sh

Color text on terminal using Shell script Color text on terminal using Shell script

References

bash:tip_colors_and_formatting

Stackoverflow: Change echo output

Terminal color escape sequences

This post is licensed under CC BY 4.0 by the author.