Creating colorful bash output using the ANSI color codes

“Even the simplest shepherd modestly watching his sheep under the stars would discover, once he understood the part he was playing, that he was more than a servant, was a sentinel. And each sentinel among men is responsible for the whole kingdom.”
Antoine de Saint Exupèry Wind, Sand and Stars

Why bother?
Working with the shell is very satisfying. The closeness to the operating system which a shell allows offers a freedom which cannot be (in my opinion) replicated within a GUI. The shell has many helpful features, auto command completion with tab, command history, shell shortcuts (eg. CTRL+e brings the cursor to the end of a line or CTRL+k  cuts everything after the cursor to the clipboard) , to name but a few and one of these features is the ability to color the screen content. This feature can be used to personalize the shell on a user basis. E.g. red prompt for root, blue for user0, green for user1 etc. Other possibilities include coloring warning banners such as /etc/issue or /etc/motd, making script messages standout (error messages colored red), coloring texts (like todo lists) and there is always just fooling around more for fun than effect. There are a lot of tutorials available on how to color bash output. Some good, some bad. This article concerns itself with the 8 colors (not 8 bit 256 colors) which are available. The reasoning being simple, I don’t use them.

How?
Firstly you need to know how to use ANSI escape sequences. These escape sequences start (funnily enough) with the ESC (ASCII  decimal 27/ hex 0x1B/ octal 033) character, followed by two or more characters. A modern bash is quite capable of interpreting \033 (which is needed if 256 colors are to be used) but generally ANSI/VT100 Terminal Codes are incorporated in scripts (\e being the escape code). If more than two characters follow, these are preceded by a left crotchet ([). The ANSI color codes are grouped thus: text attribute code, text foreground color code, text background color code with each code separated by a semi-colon (;). The sequence is closed with the letter m. Eg. \e[0;0m resets the shell and generally terminates any color changes that have be made.
The text attribute codes are as follows:
0 : No Attribute
1 : Bold
4 : Underscore
5 : Blink
7 : Reverse
8 : Concealed

The text foreground color codes are:
30 : Black
31 : Red
32 : Green
33 : Yellow
34 : Blue
35 : Magenta
36 :  Cyan
37 : White

And finally the text background codes can be seen below :
40 : Black
41 : Red
42 : Green
43 : Yellow
44 : Blue
45 : Magenta
46 :  Cyan
47 : White

As you can see the second character (1..7) defines the color, while the first character (3|4) denotes text or background color.
Here are a few examples to help clarify.

Colorful Prompt:
Edit ~/.bashrc and add the following variable
PS1=’\e[1;32m[\u@\h \W]\$\e[0;0m ‘

The command
$ source .bashrc
makes the changes visible. In this case a bold green prompt containing the login, host and working directory, surrounded by crotchets and followed by a dollar sign.

[user@host ~]$

echo -e
This short script will run through the colors on the prompt. There are many, many similar such scripts to be found all over the web. This example is quick and dirty and can be typed directly into a shell.

$ for i in {3..4}
> do
> for n in {0..7}
> do
> echo -e “${i}${n} \e[${i}${n}mcolor\e[0;0m”
> done
> done

Video

text.txt
It is of course possible to color simple txt files. The file text.txt contains the following text,

This is a test and can be deleted.

and this should be presented in color, lets say red, and in bold print. When using vim to color a file the ANSI/VT100 escape code ^[ replaces \e. This code is created within vim using the following key combination:
ctrl +v crtl + [

The new text.txt file will look like this:

^[[1;31mThis is a test and can be deleted^[[0;0m

Using cat the contains are displayed red and in bold print.

$ cat text.txt

This is a test and can be deleted.

Video

Public Domain Mark