How to Set a Custom Bash Shell Prompt String (PS0/PS1/PS2/PS3/PS4) in Linux

Tutorial Jan 02, 2021

Bash is the default shell program running on most of our Linux terminals. On most Linux distributions, by default, when launching a new terminal window, we are greeted with the [email protected]:/current_directory$ text. This is called a Prompt String.

Default bash shell Prompt String (PS1) on Ubuntu

This is a tutorial on how to customize the bash shell Prompt String in Linux.

WHAT IS PS0, PS1, PS2, PS3 and PS4

If we look at the man page of bash, we'll see these lines in it.

PS0    The  value  of  this parameter is expanded (see PROMPTING below)
       and displayed by interactive shells after reading a command  and
       before the command is executed.
PS1    The  value  of  this parameter is expanded (see PROMPTING below)
       and used as the primary prompt string.   The  default  value  is
       ``\s-\v\$ ''.
PS2    The  value of this parameter is expanded as with PS1 and used as
       the secondary prompt string.  The default is ``> ''.
PS3    The value of this parameter is used as the prompt for the select
       command (see SHELL GRAMMAR above).
PS4    The  value  of  this  parameter  is expanded as with PS1 and the
       value is printed before each command bash displays during an ex‐
       ecution trace.  The first character of the expanded value of PS4
       is replicated multiple times, as necessary, to indicate multiple
       levels of indirection.  The default is ``+ ''.

PS0, PS1, PS2, PS3 and PS4 are environment variables holding information related to what should be displayed on the terminal for us. The "PS" in these five variables stands for Prompt String or Prompt Statement. PS1 would translate to Prompt String One or Prompt Statement One. And like any environment variable, we can see its contents with echo,

echo $PS1
\[\e]0;\[email protected]\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

Essentially, this text translates to the familar [email protected]:/current_directory$ .

We get presented with PS1, PS2, PS3 or PS4 when bash expects a keyboard input from us. The one we are most exposed to is probably PS1. We can customize any of the 5 Prompt Strings by updating the environment variable that it uses.

Note: Executing and testing out the following export commands to update the Prompt String variables are harmless. The updated variables will not persist. Opening up a new terminal will undo the changes. Read the "MAKING IT PERSISTENT" section of this article to learn how to keep your custom Prompt Strings.

PS0

By default, the PS0 variable is empty. This Prompt String is displayed to us right after we press the Enter key and before the command gets executed. So, running the following command will let us see "Executing command..." whenever we run a command,

export PS0="Executing command...\n"
customized PS0 (Prompt String Zero)
Custom PS0

PS1

By default, Prompt String shows us the familiar [email protected]:/current_directory$ text we see on the terminal. The following command will update the PS1 variable so that we will see "$ " instead of the default one.

export PS1="$ "
customized PS1 (Prompt String One)
Custom PS1

PS2

This Prompt String is shown when we try to run multi-line commands. The following command will update the PS2 variable so that we will see "multi-line> " instead of the default "> ".

export PS2="multi-line> "
customized PS2 (Prompt String Two)
Custom PS2

PS3

This Prompt String is shown when the select command is waiting for input. The following command will update the PS3 variable so that we will see "your choice: " instead of the default #?.

export PS3="your choice: "
customized PS3 (Prompt String  Three)
Custom PS3

PS4

This Prompt String is the debugging trace line prefix for bash scripts. The following command will update the PS3 variable so that we will see "~> " instead of the default + .

export PS4="~> "
customized PS4 (Prompt String  Four)
Custom PS0

MORE PROMPT STRING CUSTOMIZATIONS

Since most of the modern Linux terminals supports Unicode, it is possible to use emojis in the Prompt String.

Prompt Strings can be more than just static text. Bash allows them to be customized by using backslash-escaped special characters. As seen on the man page of bash, they are listed below,

\a     an ASCII bell character (07)
\d     the date in "Weekday Month Date" format (e.g., "Tue
       May 26")
\D{format}
       the format is passed to strftime(3) and the result
       is inserted into the prompt string; an empty format
       results in a locale-specific time representation.
       The braces are required
\e     an ASCII escape character (033)
\h     the hostname up to the first `.'
\H     the hostname
\j     the number of jobs currently managed by the shell
\l     the basename of the shell's terminal device name
\n     newline
\r     carriage return
\s     the name of the shell, the basename of $0 (the
       portion following the final slash)
\t     the current time in 24-hour HH:MM:SS format
\T     the current time in 12-hour HH:MM:SS format
\@     the current time in 12-hour am/pm format
\A     the current time in 24-hour HH:MM format
\u     the username of the current user
\v     the version of bash (e.g., 2.00)
\V     the release of bash, version + patch level (e.g.,
       2.00.0)
\w     the current working directory, with $HOME
       abbreviated with a tilde (uses the value of the
       PROMPT_DIRTRIM variable)
\W     the basename of the current working directory, with
       $HOME abbreviated with a tilde
\!     the history number of this command
\#     the command number of this command
\$     if the effective UID is 0, a #, otherwise a $
\nnn   the character corresponding to the octal number nnn
\\     a backslash
\[     begin a sequence of non-printing characters, which
       could be used to embed a terminal control sequence
       into the prompt
\]     end a sequence of non-printing characters

On top of all this, we can use colours too.

PROMPT STRING GENERATOR

Now that we know how things are done under the hood. We can lean towards Prompt String generators to quickly create the export command we need to make our lives easier. A quick google search will bring up some online ones. The one I'm fond of is bashrcgenerator.com. It has a drag and drop interface and can customize each element's colours.

MAKING IT PERSISTENT

The export command for our custom Prompt String needs to be added to the .bashrc file in our home folder for it to persist on to newly opened terminals. The process is straight forward, first open the .bashrc file in a text editor like vim or nano,

vim ~/.bashrc

Then add the export command(s) to the end of the file, each on their own line if there are more than one.

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

export PS0="Executing command...\n"
export PS1="[\W]\\$ \[$(tput sgr0)\]"
export PS2="multi-line> "

That is all! As always, Linux is awesome for letting us shape it to suit our needs and tastes. The steps involved to make the Prompt String more functional or pretty is easy once we understand how it is generated and what we need to do to modify it.

Tags

Lime

Simple, like quantum physics.