Customizing and coloring the bash prompt

If you’re already not familiar with bash, the prompt is the little blurb of text displayed when it asks for a command. It capable of communicating a lot, but most people don’t use it quite fully. Do you want to add some color to the rather bland CLI interface? Or do you need to get information like the current git branch you’re on? This article will show you how to spruce up bash’s shell prompt.

A very basic prompt

To keep things simple, let us write out a very simple prompt – we will display >>> instead of the usual prompt. Type in the following at the very end of your ~/.bashrc file:

PS1='>>> '

To see the changes in action, fire up a new terminal window, or you can type in source ~/.bashrc in an existing terminal window.

You will now see something like this:

>>> _

Congratulations, you’ve just customized your bash prompt. So, how does that work? The bash prompt is controlled by a variable named PS1, and we’ve just set it to >>>. Setting the PS1 variable is all you need to do to customize your prompt.

By the way, if you want to make these changes available for all users on the system, you need to use /etc/bash.bashrc (on Debian and Ubuntu) or /etc/bashrc (on other distributions) instead of ~/.bashrc.

Having that sorted out, let us see how to make more useful custom prompts.

Escape sequences

Bash allows you to use some shortcuts to retrieve details, such as the user name, the machine name, the current time, and so on. These shortcuts are called escape sequences.

Let’s say, you want to display the user’s name and the current directory, followed by a $. It can be retrieved with the \u and \w escape sequences respectively. Thus, our PS1 would look like:

PS1='\u \w $ '

This ends up looking like this:

john ~ $ _

If you wanted to display the current time in addition to the other details, and display the $ on another line entirely, you could do something like so:

PS1='[\t] \u \w\n$'

And sure enough, our prompt has it:

[01:13:55] john ~
$ _

The \t shows the current time, and the \n moves everything after it (in this case, the $) to the next line.

Here’s a list of the most commonly used escape sequences:

\u

The current user

\w

The current working directory

\W

The last fragment of the current working directory. For example, if you are currently in /usr/local/bin, this will give you bin.

\h

The name of the computer, upto a dot(.). For example, if your computer is named ubuntu.pc, this gives you ubuntu.

\H

The full hostname

\d

The date in “Weekday Month Date” format (e.g.”Tue 21 July”)

\t

The current time in 24 hour HH:MM:SS format

\T

The current time in 12 hour HH:MM:SS format

\@

The current time in 12-hour AM/PM format

\n

Move on to the next line.

Adding color and styling

Most people would like some color on the rather dull shell prompt. This can be achieved with the help of ANSI escape sequences in the PS1 variable. These escape sequences need to be enclosed in \[ and \] in order to work properly.

Suppose, you want to show the username in red and the directory path in cyan, followed by a yellow, bold $ symbol. The escape sequences for red is \e[31m , for cyan, it is \e[36m, and for yellow, it is \e[33m. For bold text, we need \e[1m. In addition, we need the reset ANSI escape sequence, which prevents styles from affecting the rest of the text in the shell. The reset sequence is \e[0m.

Advanced customization with command substitution

The customization discussed so far are pretty basic in nature. What if we want to do more advanced things?

It turns out that we can assign values of PS1 conditionally by using the command substitution construct. Text enclosed within a $(...) is evaluated as a command, and then, its output is put directly its place.

A pretty common use case would be to display the current git branch you’re on in your project directory. You can easily get the current branch with:

git branch 2>/dev/null | sed -n "s/* \(.*\)/\1/p"

If you’re already familiar with command substitution, you might be tempted to write something like this:

PS1="\u \w $(git branch 2>/dev/null | sed -n "s/* \(.*\)/\1 /p")$ "

However, this won’t work. This is because, the command substitution construct is evaluated at the time of reading .bashrc. When you move across directories or change git branches, the command substitution isn’t re-evaluated.

However, it turns out that if the $(...) is present as-is inside the PS1 variable, it will be re-evaluated every time the bash prompt is shown. If you’re using single quotes, it’s easy to do this:

PS1='\u \w $(git branch 2>/dev/null | sed -n "s/* \(.*\)/\1 /p")$ '

When you’re using double quotes, you need to escape the $ symbol like so:

PS1="\u \w \$(git branch 2>/dev/null | sed -n "s/* \(.*\)/\1 /p")$ "

Phew! So now that we’ve sorted out how to get things working, let us see how things actually look like. When you are in a directory that is not a git repository, the prompt will appear as:

john ~ $ _

However, when you are in a directory containing a git repository, the prompt will change:

john ~/my-awesome-project master $ _

That was a rather simple example. However, it is possible to take this concept further and design more complex prompts.

Example: smileys on the prompt

Say, you want to display a green :) if the previous command succeeded, and a red :( if it failed. The $? variable holds the return value of the previous program — it contains a 0 if it succeeded, and a non-zero value if it failed. You could do something like:

Unicode characters

Unicode is a standard way of representing text in a compatible way across various platforms. Don’t let the bit about “text” fool you though — it can even represent symbols and emojis!

To display a Unicode character in the prompt, you should first know its hexadecimal (hex) representation. Given a particular Unicode character, you can get its hex representation by using xxd or hexdump -C like so:

However, depending upon the terminal emulator and the font used, you may see monochrome emoji or even garbled characters.

Customizing other prompts

Here’s something that most people don’t know — bash has other prompts, and they can be customized too! We have already seen how we can control the main prompt by setting the PS1 variable. Similarly, the PS2 variable controls the prompt that’s shown while entering a multi-line command.

The default multi-line prompt is simply a >, and you can see this by typing:

$ ls \
> -lR

Try modifying PS2 and see it in action:

$ PS2='==> '
$ ls \
==> -lR

Armed with these tricks, you can now fully customize your bash prompt!