Tag Archives: colour

Bash Colours

Example:
echo -e "\e[1;34mThis is a blue text.\e[0m"

Explanation:
Bash uses numeric codes to set attributes of the text to be displayed.

Attribute codes:
00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed

Text colour codes:
30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white

Background colour codes:
40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white

In the example above, I used the ANSI escape sequence \e[attribute code;text colour codem to display a blue text. Therefore, we have to use -e option in calling echo to escape the input. Note that the colour effect had to be ended by \e[0m to prevent the display of the prompt with different colours. However, the effects are sometimes interesting. I encourage you to play around with it.

To have a background, we must use the background colour codes. The sequence then becomes \e[attribute code;text colour code;background colour codem. As you can guess, any missing code is simply taken as zero value by bash. Provided you shell supports 8-bit colours (the new Cygwin version 1.43 and a00.seng.engr do), you can display a coloured welcome message when you log into bash should you wish to.