Why Does Command Gives the Shell Prompt Back Linux

This tutorial explains how to configure or customize the Linux shell prompt in detail with practical examples. Learn how to change the default Linux shell prompt temporarily and permanently.

Understanding Linux shell prompt

A Linux shell prompt is the place where we enter the command and by default receives the output of that command. It consists of a set of information that appears each 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 pound sign (for the root user). By default, it includes username, hostname and current working directory.

understanding default shell prompt in linux

If require, we can customize this information. This information is controlled by an environmental variable named PS1. To view the current value of this variable, we can use following command.

echo PS1

You may wonder how the shell prompt generates from this string, as it only contains a few of the characters which we see at our prompt such as square brackets, at (@) sign and dollar sign.

What about the reset of the information which we see at shell prompt and what are these backslash-escaped characters in this output string?

A character followed by a backslash has a special meaning in shell processing. While processing the string stored in PS1 environmental variable, if shell sees a character followed by a backslash, it uses the special meaning of that character instead of the character itself. If character is not followed by a backslash, shell uses it as it is.

Following table lists the special characters and their meaning in shell processing.

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

Now we know the meaning of the backslash-escaped special characters, let's decode the shell prompt string again. As above output shows, default shell prompt string in RHEL is following.

Following table explains each character.

prompt string [ \u @ \h \W ] \$
type of character Regular character Special character Regular character Special character Special character Regular character Special character
value in processing [ username @ hostname without domain name last directory in current working directory path ] User type. $ for regular user and # for root user

shell prompt explained

Unlike RHEL which uses a simple and straightforward shell prompt, Ubuntu uses a more customized version of shell prompt. It not only customizes the information but also personalizes the color and appearance of the prompt.

Unless you are interested in color and appearance of the prompt, you can skip the values which are surrounded by the backslash-escaped square brackets (\[).These values are used to customize the style of the prompt.

Following figure shows the shell prompt string on Ubuntu system.

default shell prompt in ubuntu

Characters which are used in customizing the style of the prompt are underlined with blue color and the characters which are used in building the prompt are underlined with yellow color.

Changing shell prompt

There are two ways to change the shell prompt; temporarily and permanently. Changes made through the temporary method apply only in current shell session. At next login, default shell prompt restores automatically. Changes made through the permanent method do not apply in current shell session but they apply on all further sessions.

Changing shell prompt temporarily

Since the shell prompt is built from the environmental variable PS1, changing its value will also change the shell prompt. Before we change it, let's take the backup of existing value.

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.

backup default shell prompt

Now we have taken the backup of existing shell prompt string, let's make some changes in it and see how those changes affect the shell prompt.

Making a hidden shell prompt

If we assign nothing to the prompt string or make it blank, we will get a blank shell prompt.

Since nothing is visible at shell prompt, you can trick friends with this hidden shell prompt. From functional point of view, this is the same shell prompt. Trick's apart you should not use such a disconcerting prompt. To get the original shell prompt back, assign the original shell prompt string again from backup.

hidden shell prompt

Customizing shell prompt with special characters

Before we use special characters in customizing the shell prompt, let's understand the two important points.

Shell treats a character as special character only if it followed by a backslash and surrounded by quotes. For example, to display time in 24-hours format if we use, PS1=\A it will be wrong. Instead of displaying time, shell will treat the character A as a regular character and will use it as it is.

While defining a shell string, always use a white space before closing the quote. For example, to define the string \A use, "\A " instead of "\A".

how to assign shell prompt string correctly

Making a UNIX style prompt

Following string sets the shell prompt to display only a single user type identification character (# for root user and $ for regular user).

This type of prompt is commonly used in UNIX.

how to get unix stype $ prompt in Linux

Have you noticed? I did not use a white space before closing the quote. And in result, as excepted, there is no space between entered command and command prompt. To avoid the confusion between the command prompt and the entered command, always use a white space in shell prompt string before closing the quote.

Displaying command number in shell prompt

To display the command number in history and in current shell session, use the options \! and \# respectively.

using command number as shell prompt

Displaying current time in shell prompt

To display time in following formats, use the options \@, \A, \t and \T respectively.

  • 12-hours format (Hours: Minutes AM/PM)
  • 24-hours format (Hours: Minutes)
  • 24-hours format (Hours: Minutes: Seconds)
  • 12-hours format (Hours: Minutes: Seconds)

using current time as shell prompt

Using current date as shell prompt

To use current date as a shell prompt, use character \d. Shell displays date in 'Day, Month, date' format.

current date as shell prompt

Using shell name as shell prompt

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

shell name as prompt

Using shell version as shell prompt

To display only shell version, use \v character and to display shell version number with release number, use \V character.

using shell version as shell prompt

Using username as shell prompt

To use the username of current user as shell prompt, use \u character.

using username as shell prompt

Using current working directory as shell prompt

Shell gives us two choices to use current working directory as shell prompt. We can use full path of the current working directory or can use only the last directory from the path. To display full path, use \w and to display only the last directory, use \W.

displaying current working directory as shell prompt

Using hostname as shell prompt

We can use both types of hostname for shell prompt; with domain name and without domain. To display hostname with domain name, use \H while to display it without domain name, use \h.

using hostname as shell prompt

Changing shell prompt permanently

Once you know how to change the shell prompt temporarily, changing it permanently becomes piece of cake. To change it permanently, put your desired shell prompt string in the end of the file .bashrc.

Let's take an example. Above, we changed the shell prompt in UNIX style temporarily. To make this change permanent, open .barshrc file and in the end of the file add the same string that we used in making temporary change. Save the file and logout from current shell session.

changing shell permanently

From next login session, we will get new shell prompt by default.

shell changed permanently

To revert back to the default shell prompt, open .bashrc file again and remove the line that we added.

That's all for this tutorial. If you like this tutorial, please don't forget to share it with your friends through your favorite social platform.

Why Does Command Gives the Shell Prompt Back Linux

Source: https://www.computernetworkingnotes.com/linux-tutorials/customize-or-change-shell-command-prompt-in-linux.html

0 Response to "Why Does Command Gives the Shell Prompt Back Linux"

Mag-post ng isang Komento

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel