Linux – Updating terminal prompt color scheme

One of the great things about Linux is how customizable it is. One customization that I like to make on my Linux systems is to update the color scheme of my terminal prompts, so that I can quickly view the current context of my shell. This can be especially helpful when I’m hopping between separate shells or SSH session windows.

There are a few ways to update the color scheme of your Linux terminal prompt. In this article, I will provide a ‘crash course’ explanation to help you customize your terminal prompt quickly and easily by editing the .bashrc file.

How the terminal prompt is configured

The default prompt for all users is configured in the /etc/bashrc file, which usually results in a default prompt that looks like so:

However, each user has a .bashrc file in the root of their home direction (~) which can override the defaults set in /etc/bashrc for their specific shell.

The $PS1 environment variable

The $PS1 environment variable is used to configure the layout and coloring of the terminal prompt.

As an example, the $PS1 string variable that I use for a non-root user on my Linux systems is below:

PS1="\[\e[0;m\][\[\e[1;32m\]\u\[\e[0m\]@\[\e[1;33m\]\h \[\e[1;36m\]\w\[\e[0;m\]]$ "

This results in a terminal prompt that looks like so:

You can utilize bash prompt special characters to define the layout of your $PS1 string variable. Using my ‘sudouser’ string as an example:

PS1="\[\e[0;m\][\[\e[1;32m\]\u\[\e[0m\]@\[\e[1;33m\]\h \[\e[1;36m\]\w\[\e[0;m\]]$ "
    ----- ---  -     -----  --     --  -     -----  --      -----  --     ---  ----
      ^    ^   ^       ^    ^      ^   ^       ^     ^         ^    ^       ^     ^
      |    |   |       |    |      |   |       |     |         |    |       |     |
See Note1  |   | BoldGreen* |    Grey  |  BoldYellow |    BoldCyan  |     Grey    |
           |   |            |          |             |              |             |
         Grey  |       Username   @ character   Hostname      Working Dir**    See Note2
               |
          [ character

Note1

  • "  –  Declares the start of a string
  • \[\e[  –  Tells terminal to not print the following characters (open escape sequence)
    • \] is the matching close escape sequence

Note2

  • ]  –  Literal character (closes the prompt bracket)
  • $  –  Typical prompt character for a non-root user
  •   (space)  –  Offsets the cursor from the prompt by 1 space
  • "  –  Closes the string declaration

*The first value in the color code (X;XXm) regards text transformation. 0 is normal, 1 is bold. The second value in the color code (X;XXm) is the actual color value. All color codes need to be ended with the m character. There are dozens of guides online that list all of the transformation and color options available.

**\w (lowercase) prints the current working directory, with symbols, if applicable. For example, ~/git\W (uppercase) prints the full path. For example, /home/sudouser/git.

Once you have configured your $PS1 string variable, you need to update your .bashrc file.

Back up your original .bashrc file

It’s best practice whenever modifying a system file to make a backup of the original. In the event something goes sideways, you can roll back to the original file quickly.

cp ~/.bashrc ~/.bashrc.orig

Configure your .bashrc file

Using your favorite text editor, open the .bashrc file, which lives in the root of your home directory.

vi ~/.bashrc

Paste the full $PS1 string variable into the file. An example .bashrc for a centOS user is below.

NOTE: Since the ‘PS1=’ string is a variable declaration, we do not pre-pend the $ to the variable name. The pre-pended $ is only needed when referencing the variable.

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
       . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions
PS1="\[\e[0;m\][\[\e[1;32m\]\u\[\e[0m\]@\[\e[1;33m\]\h \[\e[1;36m\]\w\[\e[0;m\]]$ "

Save and exit the file. Now close and re-open your shell to see your custom terminal prompt!

Optional: Update the .bashrc file for your root user

To differentiate my shell when I am logged in as the root user, I use the $PS1 string variable below:

PS1="\[\e[0;m\][\[\e[1;31m\]\u\[\e[0m\]@\[\e[1;33m\]\h \[\e[1;36m\]\w\[\e[0;m\]]# "

This results in a terminal prompt that looks like so:

Append the $PS1 variable string to /root/.bashrc

Other Customization Options

Customization options for your terminal prompt don’t end with colors. You can play around with bash special characters to add the current system time, run file counts on the current working directory, etc.