Skip to main content

Colouring Shell Output

·150 words·1 min
Code Bash

Quite simply, there are a sed of ANSI escape codes that can be interpreted by bash or other good shells that can allow for nice colourization of output.

However, caution must be exercized, as with the use of echo, the -e option must be utilizied to allow for them to work. printf has no problems with them otherwise.

#!/usr/bin/env bash

RED='\033[0;31m'
GREEN='\033[0;32m'
NO_COLOUR='\033[0m'

printf "${RED}printf RED\n"
printf "printf ${NO_COLOUR} reset\n\n"

echo -e "${GREEN}echo GREEN"
echo -e "echo ${NO_COLOUR} reset"

alt text
Example of coloured shell output in a termain view

A simple set of colours are listed below:

Black        0;30     Dark Gray     1;30
Red          0;31     Light Red     1;31
Green        0;32     Light Green   1;32
Brown/Orange 0;33     Yellow        1;33
Blue         0;34     Light Blue    1;34
Purple       0;35     Light Purple  1;35
Cyan         0;36     Light Cyan    1;36
Light Gray   0;37     White         1;37

A more complete set of codes and colours can be found on this Wikipedia page.