7/30/2007

PS shell variable

Changing your Command Prompt (the PS1 shell variable)


The UNIX shell displays a command prompt when the system is ready to accept commands on the command line. The default command prompt is a single character (typically $ or #).

Changing or customizing the command prompt makes your life easier when jumping from system to system, or when logging in as multiple users on a single system (e.g. your personal/non-privileged account and root). To customize your prompt you will need to modify the PS1 shell variable.

PS1 stands for "prompt string 1" and defines the primary prompt string. If you wanted your command prompt to contain the current username and hostname separated by the "@" character and enclosed in brackets (e.g. [root@hawk] #), PS1 would need to be re-defined with the following command(s):


export PS1="[${LOGNAME}@$(hostname)] # "

or

PS1="[${LOGNAME}@$(hostname)] # "
export PS1

The shell variable LOGNAME contains the username you logged in with, and $(hostname) will execute the hostname command which will print the name of the current host system. Exporting PS1 makes it available to any subshells you create during the login session.

Since PS1 in this example was re-defined on the command line, it will be lost as soon as you log out. To retain this definition across login sessions you will need to add the previous command(s) to your shell initialization file (.profile if the Korn shell is your default shell).

3 comments:

Yonghang Wang 说...

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
$

Yonghang Wang 说...

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.

Yonghang Wang 说...

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.