This is one of the prompts available for us in Linux/Unix. The other prompts available for us are PS1, PS2, PS3. This prompt is very much useful when debugging shell scripts using -x option for set command. This prompt should be written at start of the script so that it will be available through out the script.
To know the default PS4 prompt use echo command to see whats stored in it.

echo default PS4 prompt

echo $PS4

Sample output:
+

We can change the PS4 promte to a meaning full sentence so that it will be usefull for debugging.

Changing PS4 promt

PS4=’at Line number:${LINENO} #’

If you set above prompt and use it in shell scripts, it will help you to know which line in the script is throwing it? For example take below script where we set PS4 to ‘at Line number:${LINENO} #’

#!/bin/bash
PS4=’at Line number:${LINENO} #’
read -p “testing the test: ” VAR1 VAR2
echo “VAR1 value is $VAR1”
echo “VAR2 value is $VAR2”

Save the above file as testsh.sh and execute it by enabling debugging.

bash -x testsh.sh
+ PS4=’at Line number:${LINENO} #’
at Line number:3 #read -p ‘testing the test: ‘ VAR1 VAR2
testing the test: asdfas asdfasd
at Line number:4 #echo ‘VAR1 value is asdfas’
VAR1 value is asdfas
at Line number:5 #echo ‘VAR2 value is asdfasd’
VAR2 value is asdfasd

If you see above example it’s very much usefull when executing scripts. PS4 supports System variables defination in it’s prompt as well as some special charecters as shown 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
And we can do many things with this PS4 prompt. We will see them in our comming posts in detail.

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.