PS2(Prompt String 2) is one of the prompts available in Linux/Unix. The other prompts are PS1, PS3 and PS4. This is very much useful for entering a large command in multiple lines and when you execute incomplete command, this prompt will come into picture.

Check what your default PS2 prompt by executing below command:

echo $PS2

Output:
>

Example1: We can change this prompt to different one with a bit meaning such as “continue your command here” etc as shown in below example.

PS2=’Continue here..! ‘

Now start executing incomplete command as below.

echo “How are you 
Continue here..! and what you do
Continue here..!ok let us end it here..”

How are you
and what you do
ok let us end it here..

If you see the prompt changed from ‘>’ to ”Continue here..!’. this will be very handy and more informative when dealing with a command which spreads on multiple lines.
Example2: Below prompt will give you more information when typing data as your log in name and server where you log in.

PS2=’u@h ::  ‘  

Set the above PS2 prompt and check it your self with following data
Example output:

[surendra@linuxnix.com ~]$ PS2=’u@h :: ‘
[surendra@linuxnix.com ~]$ echo “This is how

surendra@linuxnix.com :: it works
surendra@linuxnix.com :: buddy”
This is how
it works
buddy

Other special characters like u and h includes below strings

d the date in “Weekday Month Date” format (e.g., “Tue May 26”)
e an ASCII escape character (033)
h the hostname up to the first .
H the full hostname
j the number of jobs currently run in background
l the basename of the shells terminal device name
n newline
r carriage return
s the name of the shell, the basename of $0 (the portion following the final slash)
t the current time in 24-hour HH:MM:SS format
T the current time in 12-hour HH:MM:SS format
@ the current time in 12-hour am/pm format
A the current time in 24-hour HH:MM format
u the username of the current user
v the version of bash (e.g., 4.00)
V the release of bash, version + patch level (e.g., 4.00.0)
w Complete path of current working directory
W the basename of the current working directory
! the history number of this command
# the command number of this command
$ if the effective UID is 0, a #, otherwise a $
nnn the character corresponding to the octal number nnn
a backslash
[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
] end a sequence of non-printing characters


Explore above special characters and use them whichever suit’s best for you.

Example3: Setting the line number to the command you are executing

[surendra@linuxnix.com ~]$PS2=’${LINENO} ::’
[surendra@linuxnix.com ~]$echo “How are you
25 ::mand
26 ::”
How are you
mand

Combining above two example you will get a good combination

[surendra@linuxnix.com ~]$ PS2=’u@h :: ${LINENO} ::’
[surendra@linuxnix.com ~]$ echo “how are you

surendra@linuxnix.com :: 29 ::buddy
surendra@linuxnix.com :: 30 ::this is what
surendra@linuxnix.com :: 31 ::I am saying”
how are you
buddy
this is what
I am saying

Make this prompt(PS2) permanent by editing ~/.bashrc file for a BASH Shell. In our next post we will see how to use PS3 prompt in Shell scripts.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.