Customize or change Shell command prompt in Linux

A Linux shell prompt allows us to enter a command and receive the output of that command. It consists of information that appears every time the shell is ready to accept a command. The information is surrounded by brackets and followed by a dollar sign (for regular users) or a hash sign (for the root user). By default, it includes username, hostname, and current working directory.

Unless we change the user account, the username remains the same for the entire session. It changes only when we switch the user account or log out from the current user account and log in with another user account. The computer name also remains static throughout the session. It changes only when we change the computer name. The name of the current working directory dynamically changes when we change the directory.

If required, we can customize this information. An environmental variable called PS1 controls this information. The following command displays the current value of this variable.

#echo $PS1

ps1 variable

It saves information in the form of escaped characters. A backslash sign (\) changes a regular character or symbol into an escaped character. An escaped character has a special meaning for Shell. While processing the string stored in the PS1 environmental variable, Shell uses the special meaning of escaped characters instead of their original value.

regular and escaped characters

The following table lists the meaning of the special characters.

Special character    Description
\!        Display the history number of the entered command
\#       Display the command number of the entered command in current shell session
\$       Display a $ sign for all users except the root user. For the root user, it shows the # sign.
\\        Display a backslash character
\[ … \] Customize the terminal-specific settings such as color and font style
\@      Display current time in 12-hour format (hours: minutes AM/PM)
\a       Make a beep sound (ASCII bell)
\A       Display the current time in 24-hour format (hours: minutes )
\d       Display the current date in the day, month, and date format
\h       Display the hostname without the trailing domain name
\H       Display full hostname with the trailing domain name
\j        Display the number of jobs running in the current shell session
\l        Display the name of the current terminal device
\n       Insert a new line
\r        Insert a carriage return
\s       Display the name of the shell
\t        Display the current time in 24-hour format (hours: minutes: seconds)
\T       Display the current time in 12-hour format (hours: minutes: seconds)
\u       Display the username of the current user
\v       Display the version number of the shell
\V       Display version and release numbers of the shell
\w       Display the full path of the current working directory
\W      Display only the last directory from the path of the current working directory

The default value of the $PS1 variable on RHEL is the following.

[ \u @ \h \W ] \$

It contains three escaped characters: \u, \h, and \W. The following table explains the meaning of these characters.

Default value [ \u @ \h \W ] \$
Type of character Regular character Escaped character Regular character Escaped character Escaped character Regular character Escaped character
Used value [ Username @ Hostname without the domain name Last directory in the current working directory path      ] User type. $ for a regular user and # for a root user

Customizing the shell prompt on Ubuntu

Ubuntu uses a more customized shell prompt. It not only customizes the information but also personalizes the color and appearance of the prompt. Unless you are interested in the color and appearance of the prompt, you can skip the values surrounded by the backslash-escaped square brackets (\[). These values customize the style of the shell prompt.

default shell prompt in ubuntu

In the above image, I underlined the characters that customized the prompt style with the blue color and the characters that built the prompt value with the yellow color.

Changing the shell prompt

There are two ways to change the shell prompt: temporarily and permanently. The first method updates the shell prompt only in the current session. The shell restores the original prompt when you start the new session. The second method changes it permanently. However, it applies the change at the next restart.

Changing the shell prompt temporarily

Changing the value of the $PS1 variable updates the shell prompt. The following command creates a backup of the existing value.

#PS_ORG="$PS1"

The above command creates a new variable called PS_ORG and assigns the value of PS1 to it. We can verify it by using the echo command.

#echo $PS_ORG

creating backup

Creating a backup of the $PS is optional. When you exit the session or restart the system, Shell automatically restores the original value.

Making a hidden shell prompt

To make a hidden command prompt, set the value of this variable to none. It hides the value we see on the command prompt, making it invisible.

#PS1=

Since nothing is visible at the shell prompt, you can trick friends with this hidden shell prompt. To get the original shell prompt back, assign the original shell prompt string again.

hidden shell prompt

Customizing the shell prompt with special characters

The shell treats a character as an escaped character only if it is followed by a backslash and surrounded by quotes. For example, if we set the value to PS1=\A to display time in 24-hour format, the shell treats the character A as a regular character and uses it as it is.

We also need to use a white space before closing the quote. For example, to define the string \A, we use "\A " instead of "\A".

how to assign shell prompt string correctly

Making a UNIX-like prompt

The following command changes the shell prompt to a UNIX-style shell prompt. UNIX includes only the user type (# for root user and $ for regular user) in the shell prompt. It does not display any extra information about the system.

#PS1="\\$"

how to get unix stype $ prompt in Linux

Have you noticed? I did not add a white space before closing the quote. As a result, there is no space between the entered command and the command prompt. To keep the command prompt separate from the entered command, always add a white space in the shell prompt string before closing the quote.

Displaying the command number in shell prompt

The  \! string displays command number in history. The \# string displays the command number in the current shell session.

#PS1="\! "
#PS1="\# "

using command number as shell prompt

Displaying the current time in the shell prompt

The following escaped characters display the current time in the shell prompt.

 \@              12-hour format (Hours: Minutes AM/PM)
 \A                24-hour format (Hours: Minutes)
 \t                 24-hour format (Hours: Minutes: Seconds)
 \T                12-hour format (Hours: Minutes: Seconds)

using current time as shell prompt

Using the current date as the shell prompt

To use the current date as a shell prompt, use the escaped character \d. The shell prompt displays the date in the Day, Month, and date format.

current date as shell prompt

Using the shell name as the shell prompt

To use the shell name as the shell prompt, use the \s escaped character.

shell name as prompt

Using the shell version as the shell prompt

To display only the shell version, use the \v escaped character. To display the shell version number with the release number, use the \V escaped character.

using shell version as shell prompt

Using the username as the shell prompt

To use the username as the shell prompt, use the \u escaped character.

using username as shell prompt

Using the current working directory as the shell prompt

Shell gives us two options to use the current working directory as a shell prompt. We can use the full path of the current working directory or only the last directory in the path. The \w displays the full path. The \W displays only the ending directory.

displaying current working directory as shell prompt

Using the hostname as the shell prompt

There are two types of the hostname: with the domain name and without the domain. The \H displays the hostname with the domain name. The \h displays the hostname without the domain name.

using hostname as shell prompt

Changing the shell prompt permanently

To change the shell prompt permanently, put your desired string at the end of the file .bashrc.

Let us take an example. Above, we temporarily changed the shell prompt to UNIX-style. We can make this change permanent by adding that string at the end of the .bashrc file. Save the file and log out from the current shell session.

changing shell permanently

We will get the new shell prompt from the next login session.

shell changed permanently

To revert to the default shell prompt, remove the line that we added in the .bashrc file.

Conclusion

In this tutorial, I explained how to customize the shell prompt. Using these steps, you can change the default prompt temporarily or permanently.

ComputerNetworkingNotes Linux Tutorials Customize or change Shell command prompt in Linux

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com