ANSI escape sequences consist of a sequence of characters beginning with the Escape character, character 27. The next character is often (though not always) an open-square-bracket: [
The echo command can send escape characters if you specify -e and use \e for escape.
The ANSI standard defines 8 colours, plus a bright mode, giving a total of 16 posibilities. The sequence is:
\e[<number>m
Where <number> is one of:
Foreground:
- 30 Black
- 31 Red
- 32 Green
- 33 Yellow
- 34 Blue
- 35 Magenta
- 36 Cyan
- 37 White
Background:
- 40 Black
- 41 Red
- 42 Green
- 43 Yellow
- 44 Blue
- 45 Magenta
- 46 Cyan
47 White
0 Reset all
- 1 Bold
So to make your foreground red and your background yellow:
$ echo -e "\e[31m\e[43m"
And to enable bold:
$ echo -e "\e[1m"
Of course, you can combine them all together:
$ echo -e "\e[31m\e[43m\e[1m"
There are many many other escape codes for doing other things.
For example - clear the screen and move the cursor to the top-left:
$ echo -e "\e[2J\e[1;1H"
Which is useful when changing the colour:
$ echo -e "\e[31m\e[43m\e[1m\e[2J\e[1;1H"
Which will change the colours, clear the screen, and put the cursor at the top-left. Well, almost the top left. Echo puts a carriage return in, so it moves down a line. You can add -n to echo to prevent this if you're fussy.
If you mess it all up and can't see what you're typing you can reset the terminal colours to normal by pressing:
Ctrl+v
[
0
m
Return
At what you hope is the command prompt. It will whinge about an unknown command, but you will be able to see what you're doing again.