Guides The 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

username@host:directory$

use

export PS1="u@h 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:

juliet@glade:~ $ 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
do 
	# code to handle reply
done

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

Get-MsolUser PowerShell Attributes & Properties

This article has been updated for 2020. Please note that WAAD was retired in 2018, but the cmdlets listed in this article are still...

Microsoft Azure PowerShell Scripts and Commands

Using PowerShell scripts and commands for quickly executing tasks in Windows operating systems offers a number of benefits over traditional scripting languages, such as...

Microsoft Hyper V Review

Microsoft Hyper-V: The Bottom line Microsoft Hyper-V lagged behind VMware's virtualization tool, one of the most popular tools in the space, when it was first...

Best Cloud Based Services & Companies

Any company that’s delayed introducing cloud-based software into their infrastructure needs to consider leveraging these new technologies to reap all the benefits cloud computing...

Best Load Balancers for 2020

The best load balancers are able to provide either hardware or software-based load balancing to ensure maximum service availability by offering network traffic distribution...

Related Stories