How to Customize your Ubuntu Terminal Prompt

Customize bash prompt

Most Ubuntu systems use bash or dash as their default terminal application. You type your commands at a standard command prompt, which usually displays your username, hostname, and current directory in a predefined color scheme. Bash provides many customization options for the command prompt that allow you to not only add different functions to the prompt but also distinguish them by different colors.

In this article, we will use various examples to customize and recolor our terminal application's command prompt so that you can do the same as needed. We will run the commands and procedures described in this article on an Ubuntu 22.04 LTS system.

View Current Bash Prompt Configuration

When you open your Terminal through the Dash or the Ctrl+Alt +T shortcut, you see the prompt as follows:

username@hostname:directory$

Bash prompt

This default format lists the username, the hostname, and then the current directory of the user. The bash picks up this configuration from the bashrc file that is set up individually for every user in their home directory. Here is how you can open this file:

$ nano ~/.bashrc

Location: /home/username/.bashrc

You can also use any other text editor to open this file.

This is how this configuration file looks like. We are showing you the relevant part of the file here.

The .bashrc file opened in nano

The PS1 variable that you see in the above image has all the required configuration for the bash prompt. Let us first explain what the characters in this variable indicate. You can view this variable clearly in the PS1 variable listed after the else statement.

  • \u-This indicates the username of the current user
  • \h: This indicates the hostname of the current user extracted from the fully qualified domain name.
  • \w: This indicates the current working directory. Your home directory is indicated with a tilde ~ symbol.
  • \$: This indicates if you are a normal user ($) or a root user (#).

You can also view the configuration of the PS1 variable by echoing its value as follows:

PS1 variable

Customize the Shell Prompt

After seeing where the prompt information is stored and what the PS1 variable describes, let us see how it can be edited in order to customize our bash prompt.

Before editing the PS1 variable, it is important to store its default contents in a new variable. This will help us restore the prompt to its original configuration if something goes wrong. Enter the following command in your Terminal:

$ DEFAULT=$PS1

Now the variable “DEFAULT” has all the information we need to recover our default prompt settings.

Let us now experiment some with our PS1 variable. Enter the following command:

$ PS1="\u\$ "

This new value of PS1 has the following effect on your prompt:

New Bash prompt

Now you can see only your username and root user information without any colors as no color information has been set yet.

Let us enter the following command so that our prompt also lists our working directory

$ PS1="\u:\w\$ "

This new value of PS1 has the following effect on your prompt:

New bash prompt in action

Since my working directory was home, I could only see the ~ symbol. In some other directory, say bin, my prompt will show the following information:

Result

Set The Prompt Back to Default

Since we had stored the original configuration of the PS1 variable in the DEFAULT variable, we can set the value of PS1 back to default by feeding it the value of our DEFAULT variable.

Reset shell prompt

What else can you customize?

If you want your prompt to contain a specific custom text, you can use the following sytanx:

$ PS1="[custom text] \u@\h:\w\$ "

Example:

I have set up my prompt to include a custom message as follows:

Set custom text in bash prompt

You can incorporate the following basic characters in your prompt:

Character Purpose
\d The date in day month date format.
\e The bell character.
\a The escape character.
\h The hostname of the current user till ‘.’
\H The hostname of the current user.
\l Basename of the terminal device.
\j The number of jobs being run by the shell.
\r Carriage return.
\n A new line.
\u Username of the current user.
\v Bash version.
\! Print history number of the command being run.

You can include current system time in your prompt through the following command:

$ PS1=”\A\u: \w\$ “

This new value of PS1 has the following effect on your prompt:

Show system time in command prompt

You can also customize your prompt to include an output of a command; this gives you unlimited options to incorporate in your prompt.

Syntax:

$ PS1="\u@\h on `[command]` \w\$ "

Example:

In this example, I will set the prompt to include the name of the primary group to which the current user belongs to.

$ PS1="\u@\h on `id -gn` \w\$ "

Include nam of primary group

Colorize the Prompt

After customizing the prompt, you will realize that things might look a little messed up. The plain reason is that it is not too easy to differentiate one feature from the other if they are all listed in the same color. Now let us learn how to colorize the prompt to make it more pleasing to the eye.

In order to add colors to one or more feature, the PS1 variable includes color tags. The highlighted text in the following image is a color tag.

Set colors in bash prompt

This is the format of a color tag:

\[\033[COLOR]m\]

For example, the default username and hostname that we see in our default terminal prompt are green because of the following color tag:

\[\033[01;32m\]\u@\h

You can change this color value against a bash prompt feature to give it a new color or add a new color tag to a feature that does not have any.

Here are some common colors and their values:

Color Value
Green 32
Red 31
Black 30
Blue 34
Cyan 36
Purple 35
Yellow 33
White 37

Example:

The following command will turn the prompt red as we are specifying 31(red color) in the color tag:

$ PS1="\[\033[31m\]\u@\h:\w$ "

Colorize command prompt

Use Text Styles in Prompt

You can add styles to your prompt text by assigning an attribute value to a color tag. Here is the format of a color tag with an attribute:

\[\033[ATTRIBUTE; COLORm\]

You can use the following attribute values for your prompt text:

Attribute Value Purpose
0 Normal text (This is the default value even if no attribute is set)
1 In the Ubuntu Terminal, this value specifies bold text
2 Dim text
4 Text underlining
5 For blinking text
7 Reverses text and background colors
8 For hidden text

Example:

You can use the following command to in order to underline your bash prompt:

$ PS1="\[\033[4;31m\]\u@\h:\w$ "

The value 4 indicates that we want to “underline” the text.

Use text styles in command prompt

Make Permanent Changes to the Prompt

The commands that you have executed up till now will only change the prompt for the current bash session. After you have experimented with text customization and colorization of your prompt, and reached a final that you want to set permanently for all your bash sessions, you need to edit your bashrc file.

Open the .bashrc file and copy the PS1 value you have finalized in the PS1 line under the if; then line. In the following image, I have just changed the color of my bash prompt to red:

Edit .bashrc file

Save the file by pressing Ctrl+X and then by pressing Y. The changes to your bash prompt will now be permanent. Exit the Terminal and re-open to see that your bash prompt will still be the same as you have set.

Save file

After practicing along with this tutorial, you can excel in customizing your bash prompt. You can then set up colors for differentiating the different features you have incorporated in the prompt. This way you will be able to view and make use of that useful information every time you use the bash prompt.