Your requirement

You want to produce coloured text output in your shell scripts. You have already seen this in various places (e.g. when starting the services at boot time) and have wondered how it is done.

The principle behind it

All terminal emulations common under Linux (e.g. xterm, konsole, the Linux console, PuTTY) support certain control characters that can be used to influence text attributes. You simply have to output these with echo.

How it works in detail

All control sequences are initiated with Escape [. After that comes a number, and then a lowercase m. The escape character has the Asciicode 27. To output this with echo, you need to use the option -e, then enter the sequence \033:

echo -e "Now this becomes \033[31mRed\033[0m."

Now this becomes RED.

Some important control characters

The following table gives an overview of some useful sequences. Please note that not every terminal emulation can display all attributes correctly. Sometimes, e.g. boldface, is displayed by replacing it with a higher brightness:

\033[0mReset all attributes
\033[1mBold
\033[4mUnderline
\033[5m
Blink
\033[7m
Negative
\033[30mText colour Black
\033[31mText colour Red
\033[32mText colour Green
\033[33mText colour Yellow
\033[34mText colour blue
\033[35mText colour Magenta
\033[36mText colour Cyan
\033[37mText colour White
\033[40mBackground Black
\033[41mBackground Red
\033[42mBackground Green
\033[43mBackground Yellow
\033[44mBackground Blue
\033[45mBackground Magenta
\033[46mBackground Cyan
\033[47mBackground White

Linux knowledge

These articles were written by the founder of Checkmk many years ago.
They are still valid though and thus we keep them on our website.
Mathias has since then developed a monitoring software called Checkmk

Find out more