UNIX Tutorials, Tips, Tricks and Shell Scripts

Other Prompt String Shell Variables (PS2, PS3, PS4)


PS2

When you issue a command that is incomplete, the shell will display a secondary prompt and wait for you to complete the command and hit Enter again.  The default secondary prompt is > (the greater than sign), but can be changed by re-defining the PS2 shell variable.

Example 1 - using the default secondary prompt:

$ echo "this is a
> test"
this is a
test
$

Example 2 - re-defining PS2 with a customized prompt:

$ PS2="secondary prompt->"
$ echo "this is a
secondary prompt->test"
this is a
test
$

PS3 (Korn Shell)

The select command is used for creating an interactive menu.  If you are unfamiliar with building a menu with select, it will be covered in next week's tip. 

The default prompt string used by select is #? and can be changed by re-defining PS3.  select will display the string stored in PS3 when it is ready to read the user's selection.


PS4 (Korn Shell)

A great tool for debugging shell scripts is an execution trace.  An execution trace lists each command before the shell runs it.  You can enable execution trace for your script by simply adding set -x to the beginning of it.

The default execution trace prompt is + (the plus sign), but can be changed by re-defining the PS4 shell variable:

Contents of test.debug script:

#!/bin/ksh

set -x

for i in 1 2 3
do
  echo $i
done

exit


Execution output:

$ test.debug
+ echo 1
1
+ echo 2
2
+ echo 3
3
+ exit
$

A common practice is to use the LINENO variable within the PS4 prompt to display the line number in the script that corresponds with the trace line.  For example:

Updated test.debug script:

#!/bin/ksh

set -x
PS4='[${LINENO}]+'

for i in 1 2 3
do
  echo $i
done

exit


Execution output:

$ test.debug
+ PS4=[${LINENO}]+
[8]+echo 1
1
[8]+echo 2
2
[8]+echo 3
3
[11]+exit

Notice that line numbers 8 and 11 from the test.debug script are identified in the execution trace output.

Do you need to learn UNIX or Linux, including how to read and write shell scripts? If you are ready to move past the basics, either of these online courses is a good place to start...

UNIX and Linux Operating System Fundamentals contains a very good "Introduction to UNIX Shell Scripting" module, and should be taken if you are new to the UNIX and Linux operating system environments or need a refresher on key concepts.

UNIX Shell Scripting is a good option if you are already comfortable with UNIX or Linux and just need to sharpen your knowledge about shell scripting and the UNIX shell in general.

Both courses include access to an Internet Lab system for completing the course's hands-on exercises, which are used to re-enforce the key concepts presented in the course. Any questions you may have while taking the course are answered by an experienced UNIX technologist.