The bash Linux shell provides many customization options for the prompt that you can use not only to incorporate various features in the prompt but also to differentiate them through different colors.
In this article, we will use various examples to customize and recolor the prompt of our Terminal application that will enable you to do the same depending on your needs. We have the commands and procedures described in this article on a Debian 10 Buster system.
View Current Bash Prompt Configuration
When you open your Terminal through the Debian Application Launcher search, you see the prompt as follows:
[email protected]:directory$
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 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.
- \$] 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:
$ echo $PS1
Customize Bash Prompt
After seeing where the prompt information is stored and what the PS1 variable describes, let us see how it can be edited 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:
You can only see 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:
Since my working directory was home, I could only see the ~ symbol. In some other directory, say “Pictures”, my prompt will show the following information:
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.
What else can you customize?
If you want your prompt to contain a specific custom text, you can use the following syntax:
$ PS1="[custom text] \[email protected]\h:\w\$ "
Example:
I have set up my prompt to include a custom message as follows:
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:
You can also customize your prompt to include the output of a command; this gives you unlimited options to incorporate in your prompt.
Syntax:
$ PS1="\[email protected]\h on `[command]` \w\$ "
Example:
In this example, I will set the prompt to include the name of the primary group the current user belongs to.
$ PS1="\[email protected]\h on `id -gn` \w\$ "
Colorize the Bash 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.
To add colors to one or more features, the PS1 variable includes color tags. The highlighted text in the following image is a color tag.
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 is green because of the following color tag:
\[\033[01;32m\]\[email protected]\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\]\[email protected]\h:\w$ "
Use Text Styles in Bash 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 Debian 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 underline your bash prompt:
$ PS1=”\[\033[4;31m\]\[email protected]\h:\w$ ”
The value 4 indicates that we want to “underline” the text.
Make Permanent Changes to the Prompt
The commands you have executed until 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:
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.
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.