PUBLISHED: September 18, 2019
Deprecation Notice: This article was written more than a year ago which means that its information might no longer be up-to-date. We cannot therefore guarantee the accuracy of it's contents.

Table of Contents


How to Customise the Linux Bash Prompt

One of the first things that we usually do when we start work on new Linux system is to customise the prompt. We will in this guide explain some options available for users of the bash shell.

Depending on which Linux distribution you are using, the look of the default bash prompt will vary. On a new Raspbian installation the prompt looks like this:

geek@raspberrypi:~ $

While on macOS the default prompt looks like this.

mac:~ geek$

The location of the configuration file also varies depending on the shell, but for bash users the most common location is.

~/.bashrc

Once you have located the configuration file, find the line that starts with export PS1=.

export PS1="..."

The value after the equal sign is constructed with escape characters and describes what the prompt looks like. The following cheat sheet shows some of the more commonly used escape codes.


Escape codes

username export PS1="\u "
hostname export PS1="\h "
full domain name export PS1="\H "
shell export PS1="\s "
shell version export PS1="\v "
shell release export PS1="\V "
path to current directory export PS1="\w "
current directory export PS1="\W "
$ export PS1="\\$ "
date export PS1="\d "
24 hour time export PS1="\A "
12 hour time export PS1="\@ "
24 hour time with seconds export PS1="\t "
12 hour time with seconds export PS1="\T "

Armed with this reference we will go ahead and clear the existing values before making any changes. Let us start with something basic. We will simply add our username to the prompt.

export PS1="\u "

Save and close this file. In order for the changes to come into effect we have to tell the system that the file contents has changed. This can be done by either opening a new terminal window or by issuing the source command in the current terminal session.

source ~/.bashrc

Once the file has been sourced, the prompt will immediately change.

geek

We will now add some more information to the prompt. Let us display the current working directory as well.

export PS1="\u\w "
geek~

Ok, let us customise it further by adding machine name along with the usual separator symbols.

export PS1="\u@\h:\w\\$ "

geek@raspberrypi:~$ 

That is more like it. The prompt now starts to look like what we are used to.

Using ANSI codes, we can even add colours to the prompt. The following cheat sheet, which assumes that your prompt is only made out of a $ symbol, shows some basic colours.


ANSI Colour Escape Codes


Foreground

red export PS1="\[\e[31m\]\\$\[\e[m\] "
green export PS1="\[\e[32m\]\\$\[\e[m\] "
blue export PS1="\[\e[34m\]\\$\[\e[m\] "
yellow export PS1="\[\e[33m\]\\$\[\e[m\] "
cyan export PS1="\[\e[36m\]\\$\[\e[m\] "
magenta export PS1="\[\e[35m\]\\$\[\e[m\] "
black export PS1="\[\e[30m\]\\$\[\e[m\] "
white export PS1="\[\e[37m\]\\$\[\e[m\] "

Background

red export PS1="\[\e[41m\]\\$\[\e[m\] "
green export PS1="\[\e[42m\]\\$\[\e[m\] "
blue export PS1="\[\e[42m\]\\$\[\e[m\] "
yellow export PS1="\[\e[43m\]\\$\[\e[m\] "
cyan export PS1="\[\e[46m\]\\$\[\e[m\] "
magenta export PS1="\[\e[45m\]\\$\[\e[m\] "
black export PS1="\[\e[40m\]\\$\[\e[m\] "
white export PS1="\[\e[47m\]\\$\[\e[m\] "

Summary

Armed with this knowledge we will now go ahead and add colours to the prompt.

export PS1="\[\e[32m\]\u\[\e[m\]\[\e[32m\]@\[\e[m\]\[\e[32m\]\h\[\e[m\]:\[\e[31m\]\w\[\e[m\]\[\e[37m\]\\$\[\e[m\] "
user@geekbitzone:/home/geek/ $ 

Woah, that is a quite a handful of letters and symbols! As you can see it is very easy to customise the prompt until you start to bring colours into the mix. For that reason we usually prefer to visit one of the excellent bash prompt generator sites that can help us to visually customise the prompt. Enjoy!

See Also

How to Merge and Rebase in Git
How to Merge and Rebase in Git

How to add new PDF compression filters for the Preview tool on Mac
How to add new PDF compression filters for the Preview tool on Mac

How to create PDFs with the Preview tool on Mac
How to create PDFs with the Preview tool on Mac

How to install Homebrew for Mac
How to install Homebrew for Mac

How to find out which shell I am running?
How to find out which shell I am running?

How to sync files with lftp
How to sync files with lftp

How to mirror drives with rsync
How to mirror drives with rsync

How to install a Samsung ML-191x 252x Series printer with AirPrint support on a Raspberry Pi
How to install a Samsung ML-191x 252x Series printer with AirPrint support on a Raspberry Pi

Querying the Illuminates by Default attribute in Maya
Querying the Illuminates by Default attribute in Maya

Remove nodes from a set in Maya
Remove nodes from a set in Maya



comments powered by Disqus

See also