PS3(Prompt String 3) is one of the Shell prompts available for Linux. The other prompts are PS1, PS2 and PS4. PS3 prompt is useful in shell scripts along with select command to provide a custom prompt for the user to select a value.

When using select commands it’s better to use PS3 prompt to provide meaningful information to user.
Some of the “prompt commands/Alias” available for PS3 prompt are as below.

d – the date in “Weekday Month Date” format (e.g., “Tue May 26”)
ean ASCII escape character (033)
h – the hostname up to the first .
H – the full hostname
jthe number of jobs currently run in background
lthe basename of the shells terminal device name
nnewline
rcarriage return
sthe name of the shell, the basename of $0 (the portion following the final slash)
tthe current time in 24-hour HH:MM:SS format
Tthe current time in 12-hour HH:MM:SS format
@the current time in 12-hour am/pm format
Athe current time in 24-hour HH:MM format
uthe username of the current user
vthe version of bash (e.g., 4.00)
Vthe release of bash, version + patch level (e.g., 4.00.0)
wComplete path of current working directory
Wthe 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 $
nnnthe 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

Below is a select command script which do not use PS3 prompt in it.

#!/bin/bash
select var1 in abc ced efg hij
do
echo “Present value of var1 is $var1”
done


Save the above file as selectexe.sh and start executing above script as shown below.

bash selectexe.sh
1) abc
2) ced
3) efg
4) hij
#? 1
Present value of var1 is abc
#? 2
Present value of var1 is ced
#? 3
Present value of var1 is efg
#? 4
Present value of var1 is hij
#?


If you see you are prompted with a prompt: ‘#?’ to enter a choice, This is default prompt used by select command which is assigned to PS3 variable. If you want to change this default prompt from #? to some other we can do that as well by defining PS3 before executing select command at the prompt or in script as shown below script.

#!/bin/bash
PS3=’Please enter a number from above list: ‘
select var1 in abc ced efg hij
do
echo “Present value of var1 is $var1”
done

Save the above file to selctexe1.bash and start executing it

bash selectexe.sh
1) abc
2) ced
3) efg
4) hij
Please enter a number from above list: 2
Present value of var1 is ced
Please enter a number from above list: 3
Present value of var1 is efg
Please enter a number from above list:


If you see the difference the prompt got changed from default #? to “Please enter a number from above list:”

We can use above mention control strings to give you a meaning full prompt when you are taking inputs from users. In our next post we will see how to use PS4 prompt.

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.