Colorful Prompts

Color can be set in the shell with control sequences starting with \033 and ending with m. The echo command must be given the parameter -e to interpret the control sequences:

echo -e 'Hello \033[1;31mworld\033[0m!'

The 033 is octal for 27 (decimal) which is the ASCII code for escape.

Text color

Sequence Color
\033[0;30m black
\033[1;30m dark gray
\033[0;31m red
\033[1;31m light red
\033[0;32m green
\033[1;32m light green
\033[0;33m brown
\033[1;33m yellow
\033[0;34m blue
\033[1;34m light blue
\033[0;35m violet
\033[1;35m light violet
\033[0;36m teal
\033[1;36m cyan
\033[0;37m silver
\033[1;37m white
\033[0m default

Background color

Sequence Color
\033[0m default (no background)
\033[40;m black background
\033[41;m red background
\033[42;m green background
\033[43;m brown background
\033[44;m blue background
\033[45;m violet background
\033[46;m teal background
\033[47;m silver background

Example script

for FG in 30 31 32 33 34 35 35 36 37; do
    for LI in 0 1; do
        echo -en "\033[${LI};${FG}m${LI};${FG} "
        for BG in 40 41 42 43 44 45 46 47; do
            echo -en "\033[${BG}m ${BG} "
        echo -e "\033[0m"

Text and background color can be set in one control sequence:

echo -e "Print \033[44;1;33myellow on blue\033[0m"


To use colors in prompts, just use the same codes, but enclose them in \[ and \]. These markers are required to avoid misinterpretations -- they start and end a sequence of non-printable control sequences -- probably for shells not supporting the color settings.

export PS1='\[\033[1;34m\]\u\[\033[0m\]@\[\033[0;32m\]\h\[\033[0m\]:\[\033[1;33m\]\w\[\033[0m\]$ '

This example changes the color to light blue, prints the username \u, sets the color back to default, print the character "@", changes to green, prints the hostname \h, then a colon in the default color, the working directory \w in yellow and a dollar sign in the default color.

Valuable sources: