Pavel Dolinin

Pavel Dolinin

Read more posts by this author.

Golda Me'ir 8, Hod-HaSharon Israel 4528254
2 min read

All types of prompts are configurable for your needs. You can just set the new value to dedicated PS(*) variable and pass this new value to all sub-shell by `export` command.

PS1 — environment variable for default interaction prompt, in other words this is the prompt you will see any time you login in to the system.

To get the current value of the PS1 `echo` command:

$ echo $PS

The PS1 default value for CentOS 6.5 (final) looks like:

[\u@\h \W]\$

This is very short and simple. Let’s try to translate this value: ‘[‘ — just a character to print, ‘\u’ — this will be replaced by the current user name, ‘@’ — another character to print, ‘\h’ — will be replaced by the current host-name, ‘ ‘ — one white space, ‘]’ — character to print and ‘\$’ — this character is translated as ‘$’ when you logged-in as a simple user and ‘#’ when you logged-in as a superuser (root).

As a result of this configuration your prompt will look like:

user_name@host_name current_directory $

You may need systems with more complicated PS1 values, like Ubuntu. This is example of my default PS1 in ElementaryOS:

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

So all characters which starts with ‘\’ are just formatting characters. Full list of formatting options are available in man pages of bash in PROMPTING section (here).

For this specific prompt you maybe be interested in several options: ‘\t’ — current time in 24-hour HH:MM:SS format and ‘\j’ — number of jobs currently running by this shell.

PS2 — Continuation interactive prompt. Any long command in Bash can be splitted to several lines by adding ‘\’ at the end of each of them. So at the each new line you will see this prompt. The default continuation prompt in most systems is ‘> ‘. But we can change it to something more clear like ‘continue ->’

export PS2="continue ->"

PS3 — Prompt used by ‘select’ inside shell script. In Bash there isbuilt-in called ‘select’ which helps you to create menus. For example, copy this code to some script and try how it works.

I will leave the syntax of ‘select’ command out of the scope of this post. But try to get more info from bash by:

$ help select

The default PS3 is ‘’ (empty string). Now let’s try to change it to ‘Your answer?: ’:

$export PS3="Your answer?: "

And run our script again. You will see the new prompt and the coursor waiting for your choice.

Your answer?: |

PS4 — Used by ‘set -x’ to set the prefix for tracing output. This is the parameter that defines the prompt in trace mode of Bash script execution. This may be useful when you run script with ‘set -x’ option to debug your code. For more information about Bash xtrace option look at man Bash and search for xtrace section (here).

The default value at most Bash version is ‘+’. So when you execute your script in xtrace mode you will get ‘+ ’ before the message. That includes information about variables assignment, conditional statements and others…

My PS4 preference looks like:

PS4='+ $(echo -en “$SECONDS $BASH_SUBSHELL \\033[1;36m`basename “$0"`:$LINENO $FUNCNAME()\033[0m “)'

This prompt is much informative comparing to default one. It will show the execution time in seconds, the level nesting shell, source file name with line number and the name of the function that prints the xtrace line.