Mac OS X,  Mac OS X Server,  Ubuntu,  Unix

Adding Color to & Customizing the Shell Prompt

As promised in the article on colorizing the terminal, let’s look at how to customize your bash prompt.  First note that text as well as the following can be used in your string.

  • a – ASCII bell
  • d – date
  • e – ASCII escape
  • h – LocalHostName
  • H – HostName
  • j – number of jobs managed by shell
  • l – basename of terminal device name
  • n – insert a newline
  • r – insert a carriage return
  • @ – time in 12-hour HH:MM format
  • A – time in 24-hour HH:MM format
  • t – time in 12-hour HH:MM:SS format
  • T – time in 24-hour HH:MM:SS format
  • u – current user
  • v – include bash version
  • V – include bash version & patch level
  • w – working directory, w/ $HOME
  • W – basename of working directory, w/ $HOME
  • ! –  history number of command
  • # – command number
  • $ – # if the UID is 0 or a $
  • \ –  backslash
  • [ – start sequences of non-printing characters
  • ] – used to end sequences of non-printing characters

Using the above then, you would indicate the prompt in the .bash_profile and add a new line that starts with PS1 along with the codes you’d like to use in quotes. For example, to show the path followed by a colon (:) and then the LocalHostName you could use the following:

PS1=”h : u $ “

Or to add the time in the beginning of the line:

PS1=”T : h : u $ “

To insert some text, simply place characters inside the quotes. Let’s replace the $ with the word Prompt:

PS1=”T : h : u Prompt “

Now that we’ve done a little customization let’s look at adding some color. Before doing so, consider what color and the codes that can be used per color:

  • Black – 0;30
  • Blue – 0;34
  • Light Green – 1;32
  • Green – 0;32
  • Light Cyan – 1;36
  • Cyan – 0;36
  • Light Red – 1;31
  • Red – 0;31
  • Light Purple – 1;35
  • Purple – 0;35
  • Light Brown – 1;33
  • Brown – 0;33
  • Light Blue – 1;34

Once you pick a color then let’s look at adding a little Cyan to your prompt:

export PS1=”e[0;36m[u@h w $”

export PS1=”e[0;36m ] u@h w $”

Finally, you can add multiple colors, making your prompt a little clown-like. But I am not sure that I’d recommend that (seems a little distracting). One thing I do sometimes is to set the command that I’m typing to a different color.  For example, to set it to green if that’s what you’re coming back in from
export PS1=”e[0;36m ] u@h w $ e[0;32m ]”
So customize away and enjoy!