How To Make A Custom Bash Shell Prompt

  • HOME
  • >
  • BASH
  • >
  • How To Make A Custom Bash Shell Prompt
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
Related bash posts that you may like...
5 Simple Steps On How To Debug a Bash Shell Script
Learn how to quickly debug scripts in Bash with those 5 simple steps that use some of the Bash shell special properties.
A Complete Guide to the Bash Environment Variables
A complete guide on the Linux Bash environment variables with details on how to set, unset, and use the specials shell variables or define custom environment variables.
The Most Useful Bash Shortcuts That You Will Want To Use
A detailed review of the most useful Terminal and Bash shortcuts to improve your productivity when typing command-lines and how to customize the ones you don't like.
The Complete How To Guide of Bash Functions
Learn how to write shell scripts with bash functions. This guide includes examples and best practices on how to define, call, and debug functions in bash.