Prompt Customizations

I'm a BIG advocate of customizing the prompt. I tend to do it out of selfish reasons, namely that I want information to be readily available. However, there are best practices encouraging what I am doing too. (<sarcasm>Not that we honor so called "Best Practices".</sarcasm>)

So, what information should be in a command prompt? I believe that the following three items are a minimum.

I will argue that the directory should be the full path to the working directory, not just the directory, minus parent directories.

So, if we include these three things as a minimum, plus some separation characters, we get the following:


Let's add a few of the following creature comforts in too.


The octothorpe at the start of the prompt is a nice safety net that will cause the line of text to be treated as a comment if you accidentally copy & paste the command line, including the prompt.

So, how do we set up these prompt customizations? Simple, set the PS1 variable to the following:

PS1='#[\u@\h:\w]\$ '

Note: Use single quotes rather than double quotes so you don't have to worry about escaping special characters.

Here is what (some of) the "backslash-escaped special characters" mean.

the username of the current user
the hostname up to the first ‘.’
the current working directory, with $HOME abbreviated with a tilde
if the effective UID is 0, a #, otherwise a $
an escape character
begin a sequence of non-printing characters
end a sequence of non-printing characters

I don't know about you, but I like to have my prompt in a different color, so it stands out against other lines. So, let's add some ANSI escape sequences to change the color.

PS1='\[\e[1;34m\]#[\u@\h:\w]\$\[\e[0m\] '

We've added a fair bit to PS1, but if you look you will still see the basic prompt, wrapped in ANSI escape sequences. "\e[" is the ANSI escape sequence to indicate we are changing things about the prompt. The "1;34m" means that we want to use bright (1) blue (34) as the color of the following text (m). Once we are done with the bright blue color, we have to change the color to something else. So, we again use the "\e" ANSI escape sequence to indicate we are changing something, and we use the "0m" to set the color to default (white). Since we don't want all the ANSI escape characters to mess with the length of the prompt, we wrap them with backslash-escaped special characters "\[" and "\]" to tell Bash that the characters between them are non-printing.

I personally have many xterm windows open at any given time and I'd like to do something to make them easier to identify. So, I put the text of the prompt in the window title by wrapping it in "\e]0;" and "\007". Of course I don't want these characters to mess with the Bash prompt, so they also go inside of "\[" and "\]".

PS1='\[\e]0;\u@\h:\w\007\e[1;34m\]#[\u@\h:\w]\$\[\e[0m\] '

One more thing that I find really useful is having visual feedback in the prompt if the previous command exited with an error. Of course I want this output to be in a different color, so it's extremely easy to see that there was a problem. So, I utilize Bash's PROMPT_COMMAND variable to call a function that conditionally sets the prompt in one of two ways depending on the return status of the previous command.

_prompt_command() {
	case $? in
		0) PS1='\[\e]0;\u@\h:\w\007\e[1;34m\]#[\u@\h:\w]\$\[\e[0m\] ' ;;
		*) PS1='\[\e]0;\u@\h:\w\007\e[1;34m\]#[\u@\h:\w]\[\e[01;37;41m\][$?]\[\e[01;34;40m\]\$\[\e[0m\] ' ;;
export PROMPT_COMMAND="_prompt_command"

As you can see, with a little bit of work, you can make customize your prompt so that it's extremely useful and (almost) friendly.

See also:
Shell Customizations