Colored Bash Prompt

Color you Bash Prompt

To color your prompt, use something like:

$ export PS1="\[\033[38;5;COLOR_CODEm\]PROMPT\[\033[39m\] "

where

  • COLOR_CODE is an integer representing a color
  • PROMPT is what you want to show on the prompt

For instance:

$ export PS1="\[\033[38;5;1m\]\u@\h$\[\033[39m\] "

colors the prompt green (1 is the color code for green).

List Colors

Use the following function to display the colors you have available on your terminal:

# list all colors
function list_prompt_colors {
    for i in {1..255}; do
        echo -e "This is \033[38;5;${i}m*** COLOR ${i} ***\033[39m"
    done
}

Easily Change the Color of your Prompt

Use the following function to change the color of the prompt:

# your B/W prompt!
$DUMB_PROMPT="\u@\h$ "

# color the prompt
function color_prompt {
    COLOR=$1
    export PS1="\[\033[38;5;${COLOR}m\]${DUMB_PROMPT}\[\033[39m\]"
}

(use the list_prompt_colors to get the list of all available colors.)

Increase the number of Colors Available in OSX

The OSX terminal is declared as ansi by default and ANSI terminals have only 8 colors.

If you go to the “Advanced” tab of OSX Terminal Settings, you can declare your terminal as xterm-256color, in which case the number of colors increases, as you might expect, to 256.

More Information

The Arch Linux Wiki provides a very detailed guide (on this topic and many others):