How to Change Bash Prompt

The “Bash prompt” on the command line interface is that bit of text that precedes your commands. It is usually the username followed by the hostname on most systems, so the syntax might look something like user@linuxconfig$ for example. Suffice it to say, the default Bash command line prompt on many Linux systems is quite minimal.

As we will see in this article, it can be easily changed by modifying the Bash PS{n} variables, and we can include information such as display time, load, number of users using the system, uptime and more. In this tutorial, you will see how to spruce up your Bash prompt with colors and additional information on a Linux system.

In this tutorial you will learn:

  • What are PS1 and PS2 shell variables
  • How to create custom shell prompts
  • What are the characters we can use to customize a shell prompt
How to Change Bash Prompt
How to Change Bash Prompt
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell
Other Minimal knowledge of the Bash shell. Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Bash prompt variables

Just like everything else on a Linux system, so too can the Bash prompt also be customized. We can accomplish the task by changing the values of Bash PS1, PS2, PS3, and PS4 variables. To keep the things simple, this article will be concerned just with the first two. Use the echo command to see their current values:

$ echo "Bash PS1 variable:"  $PS1
$ echo "Bash PS2 variable:"  $PS2
PS1 and PS2 bash prompt
PS1 and PS2 bash prompt

Bash PS1 prompt variable

PS1 is the primary prompt variable. Ours is currently:

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

This looks a little confusing, because a lot of it used to set the colors you see in the screenshot above. The takeaway here is the use of the special Bash characters \u (user), \h (hostname), and \w (current working directory). These types of characters are what allow us to display custom information which will change with the circumstances in the Bash prompt. More of them will be covered below.

Bash PS2 prompt variable

PS2 bash shell variable is the secondary prompt, and is usually set to >. This prompt is displayed if the shell waits for some user input, for example if you are using quotation marks and forget to close them.

$ echo "text with missing quotes
Linux Bash prompt diagram
Linux Bash prompt diagram

Bash prompt special characters

Bash prompt can be customized by using special characters. Here is a quick overview of the most used characters and their meaning:

Bash prompt special characters
Bash special character Bash special character explanation Bash special character Bash special character explanation
\a an ASCII bell character (07) \d the date in “Weekday Month Date” format (e.g., “Tue May 26”)
\] end a sequence of non-printing characters \e an ASCII escape character (033)
\h the hostname up to the first `.’ \H the hostname
\j the number of jobs currently managed by the shell \l the basename of the shell’s terminal device name
\n newline \r carriage return
\s the name of the shell, the basename of $0 (the portion following the final
\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
\A the current time in 24-hour HH:MM format \u the username of the current user
\v the version of bash (e.g., 2.00) \V the release of bash, version + patchelvel (e.g., 2.00.0)
\w the current working directory \W the basename of the current working directory
\! the history number of this command \# the command number of this command
\$ if the effective UID is 0, a #, otherwise a $ \nnn the character corresponding to the octal number nnn
\\ a backslash \[ begin a sequence of non-printing characters, which could be used to embed a
terminal control sequence into the prompt
\D{format} the format is passed to strftime(3) and the result is inserted
into the prompt string; an empty format results in a locale-specific time
representation. The braces are required

Bash prompt customization

After a user logs into the system, the user environment variables are initialized from various files:

  • /etc/profile or /etc/bashrc (system wide)
  • ~/.bash_profile , ~/.bash_login , ~/.profile , ~/.bashrc or ~/.bash_logout (user specific)
It is important to know that all user environment variables have a life time equal to the terminal session. When the terminal session is closed the user’s variables including bash shell variables defined during a terminal session are emptied and again redefined when a new terminal session is created.

In the sections below, we will define two variables to prove this statement.

Customizing Bash variable step by step instructions

We will now edit the ~/.bashrc configuration file to display a custom Bash prompt. As mentioned above, we will need to logout of our current terminal session and log back in to notice the changes taking effect or just use the source command as shown below.

  1. Use nano or some other text editor to edit the ~/.bashrc file. Alternatively, we can echo the new shell prompt settings into the file.
    $ echo "PS1='MY NEW BASH PROMPT@\\t:\\w\\$ '" >> ~/.bashrc

  2. Make the changes take effect with the following source command.
    $ source ~/.bashrc
  3. You should now see the new prompt:
    MY NEW BASH PROMPT@16:42:58:~$
    Customized Bash prompt on Linux
    Customized Bash prompt on Linux

Changing foreground and background bash prompt colors

See the table below for various colors used to change Bash shell prompt background and foreground:

Color Foreground number Background number
Black 30 40
Red 31 41
Green 32 42
Brown 33 43
Blue 34 44
Purple 35 45
Cyan 36 46
Light Grey 37 47

To change the color of your Bash prompt, the following syntax is used:

export PS1='\[\e[COLOR-CODEm\][Example]\[\e[0m\]$ '
  • \e[ = Marks the beginning of a color mod
  • COLORm = The desired color number, followed by m
  • \e[0m = Marks the end of a color mod

Some examples, using this syntax, can be seen below.

Bash Prompt Examples

  1. A red Bash prompt:
    export PS1='\e[31m[Example]\e[0m$ '
  2. A purple Bash prompt:
    export PS1='\e[35m[Example]\e[0m$ '
    Various Bash prompt examples on Linux. Adapt as needed to fit your needs
    Various Bash prompt examples on Linux. Adapt as needed to fit your needs
  3. Display the current time in Bash prompt:
    export PS1="\u@\h \t:\$ "
  4. This bash prompt displays current number of files and directories in the current directory.
    export PS1="\u@\h [\$(ls | wc -l)]:\$ "

Closing Thoughts

In this tutorial, we saw how to change the Bash prompt in the command line interface on a Linux system. Not only can this make our command line terminal look much better, but we can also use Bash’s special characters to display lots of useful information. Feel free to adapt some of our examples to your own needs, so you can get a nice looking Bash prompt with some helpful information at the same time.

Comments and Discussions
Linux Forum