Home Linux Shells How to Colorize Bash Prompt in Linux Terminal

How to Colorize Bash Prompt in Linux Terminal

The bash prompt is probably the happiest place for most Linux users. A typical Linux bash prompt will look like the following screenshot.

Linux Bash Prompt
Linux Bash Prompt

Regardless of the Linux operating system distribution, you are using, a normal/default bash prompt; like the one presented on the screenshot above, is not all that you have to deal with in terms of the default colorization.

A reference syntax to a bash prompt is straightforward and looks like the following:

user@hostname:~Some_Directory$

This article will walk us through how to colorize such a default instance of a bash prompt to any result of your liking.

Bash Prompt Variable

It is important to note that the bash prompt’s main configuration on your Linux OS can be accessed/retrieved from the .bashrc file located at ~/.bashrc.

$ ls ~/.bashrc
$ cat ~/.bashrc 
Bash Configuration
Bash Configuration

The .bashrc file settings/configurations may be different for different Linux operating system distributions.

The relative path to this file can be indicated with the ls command.

$ ls -l ~/.bashrc
User Bash Configuration File
User Bash Configuration File

As for the absolute path, you have to take note of the current Linux OS username i.e dnyce.

If we open this .bashrc file and scroll down a little bit, you will come across the following highlighted section.

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[00m\]$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$ '
fi
Bash Color Prompt Configuration
Bash Color Prompt Configuration

The first PS1 variable of this if-else statement for color prompt considers the inclusion of color information on your bash prompt. The second PS1 variable is plain simple since it does not consider color inclusion on the bash prompt.

Current Bash Prompt (PS1)

As per the above screen capture, the PS1 variable is responsible for the storage of your current bash prompt settings. The PS1 translates to the primary prompt and is the default prompt when a command is fed in to be read.

In certain scenarios, an executing command needs input like username and/or password before continuing with its execution. In this case, a secondary prompt (PS2) is activated to take care of these extra inputs.

To familiarize yourself with the current bash prompt settings of your Linux OS distribution, execute the following echo command.

$ echo $PS1
View Current Bash Prompt Settings
View Current Bash Prompt Settings

From the above screen capture, you can note notations like u, @, h, W, and $. The default setting of the above command execution for your bash prompt is as follows:

[\u@\h \W]$

Where,

  • \u is the current Linux username.
  • \h is display hostname.
  • \w is the current working directory.
  • @ links username to the hostname.
  • $ indicates that it’s either a normal or Sudoer user active.

Colorizing Your Linux Bash Prompt

You might need to save the old prompt settings before proceeding. We can save it in a variable like trueps1.

$ trueps1=$PS1

Thus, in case you mess up a step, you can return to this old value with the command.

$ PS1=$trueps1

To colorize the bash prompt, we will use the export command together with the following syntax:

‘\e[x;ym $PS1 \e[m’

where

  • \e[ starts the color scheme.
  • x;y is the applicable color pair.
  • $PS1 is the bash prompt variable.
  • \e[m ends the color scheme.

For instance, the following command will colorize your bash prompt to red:

$ export PS1=”\e[0;31m[\u@\h \W]$ \e[m ”
Colorize Bash Prompt
Colorize Bash Prompt

Red uses the color pair code 0;31 hence the execution of the above command.

To change it to brown;

$ export PS1=”\e[0;33m[\u@\h \W]$ \e[m ”

To change it to black:

$ export PS1=”\e[0;30m[\u@\h \W]$ \e[m ”
Colorize Linux Bash Prompt
Colorize Linux Bash Prompt

Reference the following table for a dynamic color range.

Color Color Pair Code
Brown 0;33
Red 0;31
Green 0;32
Black 0;30
Purple 0;35
Cyan 0;36
Blue 0;34

To make the above changes permanent, echo the set value of PS1.

$ echo $PS1

Copy it and save it as the PS1 value in the ~/.bashrc file.

PS1=”\e[0;33m[\u@\h \W]$ \e[m ”
Save PS1 in Bash File
Save PS1 in Bash File

All your bash prompt windows will now adapt to this new colorization value.

New Bash Prompts
New Bash Prompts

We can now brag about understanding how the colorization of the Linux Bash Prompt is easy.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.