A clearer bash prompt

Posted in August 2012 by under sysadmin

When working at the command line in bash, you typically see the prompt contains your username, hostname and current directory.

It is likely that if you are working on a command line you working on a remote server. Knowing which server you are on, which user you are logged in as and the current working directory are so important that it makes sense to put this information in the prompt so that you see it before running each command.

However, since I now spend more time working on my local machine, knowing that I am richard@nemesis is redundant information. It justs wastes space that could be better used. Especially when working with frameworks that have many levels of nested directories. You soon find that the prompt wraps over one line and you are left with a bit of a jumble.

Standard bash prompt is a jumble

So I have customised my PS1 prompt to show the current working directory on one line and I have a new line to enter commands into.

To make this clearer still I have labelled the two lines ⟴ (U+27F4 RIGHT ARROW WITH CIRCLED PLUS) to show the working directory and ⌨ (U+2328 KEYBOARD) to enter commands. You can find Unicode characters from the character map application.

Character Map

To make the prompt stand out from the (sometimes lengthy) output is to colour the prompt. I have chosen green which is bash colour [0;32].

Other colours are

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

The main prompt in bash is called PS1 and is an environment variable. You can see what it is currently set to by running

echo $PS1

You can override the value of PS1 in you .bashrc file which is in your home directory.

So on the last line of my .bashrc file I add

export PS1="\[\033[01;32m\]⟴  \w\n⌨  \[\033[00m\]"

And then start a new shell to see the effect. The simpliest way to do this is to open a new terminal window. You will then see the new clearer prompt.

A better bash prompt