Under Construction

The Shell Prompt

Every shell uses a visual prompt, called shell prompt, to indicate to the user that the shell is expecting input from the user. If the user starts an action or a command, the shell prompt is displayed again when the action or command ends. The shell prompt is therefore the user’s constant companion. By default, bash issues the following prompt:


The shell prompt can be customized in bash using the PS1 variable. In the following we would like to show some configuration options. However, we will keep it relatively brief, since there are many posts on the subject of bash prompts on the Internet. In particular, we will not deal with colored prompts.

First you should consider what information is so useful that it should be displayed in the bash prompt at all. Here are some suggestions:

    • Are you logged in as root, or as a normal (non-privileged user)?
    • Which user are you currently working as?
    • On which computer are you currently logged in? This is particularly important when working with a large number of systems, since otherwise it would be easy to run the wrong command on the wrong computer.
    • Which directory are you currently in??
    • What was the exit status of the last executed command?

We take up the suggestions mentioned and show how to implement them. However, what information is displayed in the bash prompt at the end, depends very much on individual needs and taste.

Under UNIX, it has become common to identify a root shell with a hash “#” (hash symbol). The dollar sign “$” is usually used for a Bourne shell or shells derived from it. The percent sign “%” is often used for a C shell or a shell derived from it. Bash provides the escape sequence “\$” which is replaced with “#” if the current user is root and replaced with “$” if the current user is not root.

You can try this out right away by setting the PS1 variable to “\$“.:

-bash-5.1$ PS1='\$ '

The escape sequence “\u” is used for the current user name. We add the username at the beginning of the prompt:

$ PS1='\u \$ '
user01 $

The computer name can be accessed using the escape sequence “\h“. We show users and computers in the form “user@host” in the prompt:

user01 $ PS1='\u@\h \$ '
user01@aixe01 $

\h” stands for the short computer name, without domains. If you want to output the fully qualified computer name (including domains), you have to use “\H“.

The current working directory can be displayed via “\w“.:

user01@aixe01 $ PS1='\u@\h \w \$ '
user01@aixe01 ~ $

In the case of deeply nested directories, however, the prompt can be a bit longer:

user01@aixe01 ~ $ cd /opt/freeware/src/packages/SOURCES
user01@aixe01 /opt/freeware/src/packages/SOURCES $

If you prefer a shorter representation, you can also use “\W” instead of “\w” for the full path specification. Then only the last path component of the current directory is displayed:

user01@aixe01 /opt/freeware/src/packages/SOURCES $ PS1='\u@\h \W \$ '
user01@aixe01 SOURCES $

No separate escape sequence is required for the exit status of the last command. This is already saved by bash in the variable “$?“. However, when using a shell variable in PS1, care must be taken to either enclose the right-hand side in a pair of apostrophes, as we did in all the examples above, or when using double quotes instead of apostrophes, one must protect the dollar sign of “$?” with a backslash “\” from being evaluated directly. We stick to the variant with an apostrophe:

user01@aixe01 SOURCES $ PS1='\u@\h \W $? \$ '
user01@aixe01 SOURCES 0 $ ls -l /does_not_exist
/does_not_exist not found
user01@aixe01 SOURCES 2 $

Since an error message is typically printed in the event of an error and the exit code is only looked at in rare cases, we do not display the exit code in our prompt. But as said above, that’s a matter of taste.

The displayed prompt and the input made by the user are not really well separated optically. The bash allows to use bold case (of course only if the used terminal supports it).

Changing the font to bold can be done using the escape sequence “\e[1m“. The font can then be reset back to normal with the escape sequence “\e[0m“. We use this and print our prompt in bold. We also pack the part with the user name, computer name and current directory in so-called square brackets (“[“, “]“):

user01@aixe01 ~ 0 $ PS1='\e[1m[\u@\h \W] \$\e[0m '
[user01@aixe01 ~] $

For this to be permanent, PS1 must be set in one of the bash startup files. We configure the prompt using the ~/.bashrc file:

[user01@aixe01 ~] $ cat .bashrc

PS1='\e[1m[\u@\h \W] \$\e[0m '

[user01@aixe01 ~] $