GuidesThe Various bash Prompts

The Various bash Prompts

It’s fairly likely that you already have a personalized setting for PS1, the default bash interaction prompt. But what about the others available: PS2, PS3, and PS4?

Tip of the Trade: Most likely, you already have a personalized setting for PS1, the default bash interaction prompt. Here’s what to do with PS2, PS3 and PS4.

PS1 is the default interaction prompt. To set it to give you

[email protected]:directory$


export PS1="[email protected] w$ "

in your ~/.bash_rc.
u is the current username, h the current host, and w the working directory. There’s a list of escape codes you can use
in the bash man page, or in the Bash Prompt HOWTO.

PS2 is the prompt you get when you extend a command over multiple lines by
putting at the end of a line and hitting return. By default it’s
just >, but you can make this a little more obvious with:

export PS2="more -> "

so it looks like:

[email protected]:~ $ very-long-command-here 
more -> -with -lots -of -options

PS3 governs the prompt that shows up if you use the select
statement in a shell script. The default is #?, so if you do nothing to
change that, the select statement will print out the options and then just
leave that prompt. Alternatively, use this:

PS3="Choose an option: "
select i in yes maybe no
	# code to handle reply

which will output:

1) yes
2) maybe
3) no
Choose an option: 

Far more readable for the user!

Finally, PS4 is the prompt shown when you set the debug mode on a shell
script using set -x at the top of the script. This echoes each line
of the script to STDOUT before executing it. The default prompt is
++. More usefully, you can set it to display the line number, with:

export PS4='$LINENO+ '

All of these can be made to be permanent changes by setting them in your
~/.bash_profile or ~/.bashrc file. (Note that this probably
makes little sense to do for PS3, which is better to set per-script.)

Latest Posts

Related Stories