Last updated:

The shell prompt is where you type commands on your terminal in an interactive shell session. The prompt can be customized to display useful information to the user like the hostname, current working directory, date, username, etc. You can change the bash prompt to display different information or even change the text colors on part or all of the prompt.

This post covers how you can customize your Bash prompt with various environment variables.

The Prompt Variables: PROMPT_COMMAND, PROMPT_DIRTRIM

The $PROMPT_COMMAND can be optionally set to a command to execute before the printing of each primary prompt ($PS1).

The $PROMPT_DIRTRIM can be optionally set to a number greater than zero corresponding to the number of trailing directory components to retain when expanding the \w and \W prompt string escapes as used by the PS variables, the characters removed are replaced with one ellipsis ().

The Prompt String Variables: PS0, PS1, PS2, PS3, and PS4

The PS variables stand for Prompt String and are used to customize various prompt messages. Those variables are expanded using certain escape sequences, see Controlling the Prompt from the Bash manual for an extensive list.

$PS0

The $PS0 variable is expanded after a command is read and before the command is executed in an interactive shell.

[me@linux ~]$ export PS0=">> "
[me@linux ~]$ sleep 3
>> 
[me@linux ~]$ echo "Hello"
>> Hello
[me@linux ~]$ sleep 3 ; echo "Hello"
>> Hello

$PS1

The $PS1 variable is the primary prompt string. It is used to define the main console prompt. The default value is \s-\v\$ .

bash-5.0$ # Default prompt
bash-5.0$ export PS1="[\u@\h \W]\$  "
[me@linux ~]$ # New prompt

$PS2

The $PS2 is the secondary prompt string. It is used to define the continuation console prompt when commands go over multiple lines in an interactive shell. The default value is > .

[me@linux ~]$ if true; then
> echo "true"
> fi
true
[me@linux ~]$ export PS2="\h >> "
[me@linux ~]$ if true; then
linux >> echo "true"
linux >> fi
true

$PS3

The $PS3 variable in Bash is used exclusively for the prompt of a bash select loop to create simple shell menus. If this variable is not set, the select command prompts with the default value of #? .

$PS4

The $PS4 variable in Bash is used for the prompt printed before the command line is echoed when the debugging shell option -x is set with the set builtin command. The default value is + . The first character of the $PS4 expanded value is replicated for each level of indirection.

[me@linux ~]$ set -x
[me@linux ~]$ echo "hello"
+ echo hello
hello
[me@linux ~]$ set +x
+ set +x
[me@linux ~]$ echo "hello"
hello
GET UNIQUE TIPS AND THE LATEST NEWS BY SUBSCRIBING TO MY NEWSLETTER.
AND FOLLOW ME ON