How to set up a Ubuntu terminal prompt

Your Ubuntu system uses bash or dash as the default terminal application. You enter your commands in response to the default prompt, which usually indicates the username, host name, and current directory in some predefined color theme. Bash provides many options for invitations that you can use not only to include various functions in an invitation, but also to differentiate them by different colors.

In this article, we will use various examples to customize and change the color of the prompts of our Terminal application, which will allow you to do the same depending on your needs. We execute the commands and procedures described in this article on the Ubuntu 18.04 LTS system.

View your current Bash Prompt configuration

When you open your terminal using Dash or the keyboard shortcut Ctrl + Alt + T, you see a tooltip as follows:

[email protected]:directory$

In this format, the default is the username, host name, and then the current user directory. Bash takes this configuration from the bashrc file, which is configured individually for each user in his 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 what this configuration file looks like. We show you the relevant part of the file here.

.Bashrc file opened in nano

The PS1 variable you see in the image above has all the necessary settings for the bash prompt. Let’s first explain what the characters in this variable mean. You can clearly see this variable in the PS1 variable specified after the else statement.

  • u-This indicates the username of the current user
  • h: Specifies the host name of the current user, extracted from the fully qualified domain name.
  • w: points to the current working directory. Your home directory is indicated by the tilde symbol ~.
  • $: Indicates whether you are a regular user ($) or a root user (#).

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

PS1 variable

Customize the shell tip

After seeing where the hint information is stored and what the PS1 variable describes, let’s see how it can be edited to customize our bash hint.

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

$ DEFAULT=$PS1

Now the DEFAULT variable contains all the information necessary to restore the default prompt settings.

Let’s now experiment with our variable PS1. Enter the following command:

$ PS1="u$ "

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

New Bash tooltip

Now you can see only your username and root user information without any colors, since the color information has not yet been set.

Let’s enter the following command so that our working directory is also indicated in our invitation

$ PS1="u:w$ "

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

New bash hint in action

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

Result

Set the prompt back to the default value

Since we saved the original configuration of the PS1 variable in the DEFAULT variable, we can return the value of PS1 to the default value by passing it the value of our DEFAULT variable.

Reset Shell Invitation

What else can be customized?

If you want your tip contain specific user textyou can use the following sytanx:

$ PS1="[custom text] [email protected]:w$ "

Example:

I configured my request to add a special message as follows:

Set custom text at Bash prompt

You can include the following main characters in your query:

symbolgoal
dDate in the format day, month, month.
eBell character.
aRescue character.
hourThe host name of the current user is up to ‘.’
HOURThe host name of the current user.
lThe base name of the terminal device.
JThe number of tasks performed by the shell.
RCarriage Return.
PNew line.
andThe username of the current user.
vBash version.
!Print the history number of the command to run.

You can enable current system time at your invitation with the following command:

$ PS1=”Au: w$ “

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

Show system time on the command line

You can also customize the prompt to enable command output; This gives you unlimited options for inclusion in your request.

Syntax:

$ PS1="[email protected] on `[command]` w$ "

Example:

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

$ PS1="[email protected] on `id -gn` w$ "

Include primary group name

Colorize tooltip

After setting up the tooltip, you will realize that everything may look a little wrong. The reason is that it is not so easy to distinguish one object from another if they are all listed in the same color. Now let’s find out how to colorize the tip to make it more pleasing to the eye.

To add colors to one or more functions, the PS1 variable includes color tags. The highlighted text in the following image is a color mark.

Set colors on the command line

This is the color mark format:

[33[COLOR]m]

For example, the default username and host name that we see in our default terminal request are green due to the following color mark:

[33[01;32m][email protected]

You can change this color value for the bash prompt function to give it a new color or add a new color label for a function that does not have it.

Here are some common colors and their meanings:

colorValue
green32
red31
the blackthirty
blue34
Cyan36
Purple35
yellow33
white37

Example:

The following command will turn the tooltip into red since we specify 31 (red) in the color tag:

$ PS1="[33[31m][email protected]:w$ "

Colorize command line

Use text styles in tooltip

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

[33[ATTRIBUTE; COLORm]

You can use the following attribute values ​​for your invitation text:

Attribute valuegoal
0Plain text (this is the default, even if the attribute is not set)
1In a Ubuntu terminal, this value indicates bold text.
2Dim text
4Underline text
5To blink text
7Changes text and background colors
8For hidden text

Example:

You can use the following command to emphasize the bash prompt:

$ PS1="[33[4;31m][email protected]:w$ "

A value of 4 indicates that we want to “emphasize” the text.

Use text styles on the command line

Make permanent changes to the tooltip

The commands you ran so far will only change the prompt for the current bash session. After you have experimented with tweaking the text and coloring your hint and reaching the final that you want to set constantly for all your bash sessions, you need to edit the bashrc file.

Open the .bashrc file and copy the value of PS1, which you completed on line PS1 under if; then the line. In the following image, I just changed the color of my bash hint to red:

Edit the .bashrc file

Save the file by clicking Ctrl + X and then clicking Y, Changes to the bash prompt will now be permanent. Exit the terminal and open again to see that the bash prompt will be the same as you set.

Save file

After learning this lesson, you can better customize the bash prompt. You can then adjust the colors to distinguish between the various functions that you included in the invitation. This way you can view and use this useful information every time you use the bash prompt.

How to set up a Ubuntu terminal prompt

Sidebar