Customizing Your Bash Prompt in Linux: Changing Colors

Last updated: December 16, 2022

One of the commonly used shell in Linux is bash. Even if there are many modern shells available, it's still the default shell in many Linux distributions.

In this tutorial, we learn how to change the color of specific parts of the bash prompt in Linux. Here we use ANSI escape codes within the PS1 variable.

Introduction to Bash shell prompt

The bash shell prompt is the text displayed on the command line before the user enters a command. By default, the prompt displays the current directory, the user's username, and the hostname of the computer.

PS0, PS1, PS2, PS3, and PS4 are environment variables that determine the appearance and behavior of the bash shell prompt. The value of these variables can be used to customize the prompt, including changing its color.

Edit the environment variables

To print the current value Bash Prompt, type:

echo $PS1
[\[email protected]\h \W]\$

To change the colors of the prompt in bash, you can use the PS1 variable, along with the \[ and \] escape sequences, to specify the colors that you want to use.

Here is an example of how you can colorize your Bash shell prompt:

export PS1="\[\e[31;42m\]\[email protected]\h:\w\$\[\e[0m\]"
change bash prompt color of PS1

In this example, the \e[31:42m sequence sets the text color to red (31) and the background color to green (42). The \[\e[0m\] sequence resets the text color to the default value. The \u and \h sequences are replaced with the user name and hostname, respectively, and the \w sequence is replaced with the current working directory.

  • \e[ - Start of color modifications.
  • Color Code+m - Select a color code.
  • \e[0m - End of color change.

You may verify the color using the echo with -e option by interpreting backslash escapes:

echo -e "\e[33mYellow Text Color\e[0m"
verify bash prompt color using echo -e

Note: The export PS1 allows only a temporary change to your Bash prompt. To make a permanent change you need to edit the ~.bashrc file.

Bash - ANSI Color Escape Codes

The following table lists some of the commonly used bash color codes:

ColorForeground CodeBackground
Black3040
Red3141
Green3242
Brown/Yellow3343
Blue3444
Purple3545
Cyan3646
Light Gray3747
White97107

Some additional characters supported by the escape sequence are:

CodeDescription
0Normal Characters
1Bold Characters
4Underlined Characters
5Blinking Characters
7Reverse video Characters

Example:

echo -e "\e[1mSample Bold Text\e[0m"
Sample Bold Text

You may also combine colors or characters to make it more elegant. For example

echo -e "\e[1;33;4;47mBrown Underlined Text on Light Gray Background\e[0m"
echo -e "\e[35;32mFancy New Text Color\e[0m"
bash prompt combine colors

Customized prompt with Special codes

In addition to using color codes to customize the Linux prompt, special codes can also be used to add information and functionality to the prompt.

By default, the command prompt is set to [\[email protected]\h \W]\$.

Each backslash-escaped special character can be decoded as follows:

  • \u : Display the current username.
  • \h : Display the hostname
  • \W : Print the base of current working directory.
  • \$ : Display # (indicates root user) if the effective UID is 0, otherwise display a $.

For a Unix user other than root, it will be displayed as below:

[[email protected] ~]$

For example, to add the current date and time to the primary prompt, the value of PS1 can be modified as follows:

export PS1="\[\e[31m\]\d \t\[\e[0m\]\s-\v\$ "

This would display the current date and time in red, followed by the shell and shell version in the default color, and ending with a dollar sign.

customize bash prompt

Here are some of the escape sequences to customize bash prompt.

  • \u  Username of the current user.
  • \w The current working directory.
  • \W The last fragment of the current working directory. For example, if you are currently in /home/ubuntu/var, this will give you var.
  • \h The name of the computer, upto a dot(.). For example, if your computer is named node01.example.com, this gives you node01.
  • \H FQDN hostname.
  • \d The date in “Weekday Month Date” format (e.g.”Tue 21 March”).
  • \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.
  • \! The history number of this command.
  • \# The command number of this command.
  • \$ If the effective UID is 0, a #, otherwise a $.
  • \j The number of jobs currently managed by the shell.

Applying Changes to the Bash shell

To make the bash prompt color or background permanent, you need to update PS1 entries in your .bashrc file. The .bashrc file is a configuration file for the bash shell that is executed every time you start a new bash session.

Open your .bashrc file in a text editor of your choice. This file is usually located in your home directory, and it contains various settings for the Bash shell.

nano ~/.bashrc

Take a backup of .bachrc file before making any changes. In your .bashrc file, find the line that starts with PS1 and contains the current value of the prompt. It should look something like this:

PS1="\[email protected]\h:\w> "
change PS1 variable in bashrc

Save the changes to your .bashrc file and close the text editor.

To apply the changes to your current Bash session, you need to source the .bashrc file by running source ~/.bashrc command.

source bashrc to make changes effective

Finally, you have a permanent bash prompt color that should now be displayed in the terminal.

Using tput command

The tput command is a terminal control tool that allows you to change the appearance of your terminal. It is commonly used in shell scripts to set various terminal attributes, such as text color and style, cursor position, and window size.

One common use of the tput command is to change the text color of your bash shell. To do this, you can use the tput setaf command followed by a number that corresponds to the desired color. For example, to set the text color to red, you can use the following command:

tput setaf 1
or
export PS1="\[$(tput setaf 3)\]\[email protected]\h:\w $ \[$(tput sgr0)\]"
tput output

This will set the text color to red until you reset it to the default color using the tput sgr0 command. You can also combine multiple tput commands in a single command line to set multiple attributes at once.

List some of the tput command line options:

  • tput bold – Bold effect
  • tput rev – Display inverse colors
  • tput sgr0 – Reset everything
  • tput setaf {CODE}– Set foreground color, see color {CODE} table below for more information.
  • tput setab {CODE}– Set background color, see color {CODE} table below for more information.

tput color codes

ColorColor Codes
Black0
Red1
Green2
Yellow3
Blue4
Magenta5
Cyan6
White7

Conclusion

The visual distinction by color is what we can do in our terminal to make our work more enjoyable and tracking. Colorizing certain information can make them stand out, and can help you locate the last prompt when scrolling through terminal history. You can even customize color patterns to distinguish your root users from others.

Now, it's time for you to utilize this effect to ease your work. I would love to read your valuable comments and suggestions on this.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin started his career in IT in the year 2000 as a computer science instructor and worked as a Linux system engineer and senior analyst roles. Currently working in DevOps environments to increase efficiency and improve delivery time in AWS Cloud infrastructure. He is certified in RHEL, CCNA, and MCP and holds a Masters's in computer science. When in his free time love playing cricket, blogging, and listening to music.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment