Need explanation for $PS1, $PS2, $PS3, $PS4


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need explanation for $PS1, $PS2, $PS3, $PS4
# 1  
Old 03-05-2010
Need explanation for $PS1, $PS2, $PS3, $PS4

Hi Masters,

could you plz. any of you explain what are $PS1, $PS2, $PS3, $PS4.


Thanks & Regards
Ramesh
# 2  
Old 03-05-2010
PS1 - to set the prompt string, ofcourse!
PS2 - to set the subshell prompt string, which by default is ">", when we enter a command half and press enter, we get the subshell prompt string which is >, then we can complete the command and press enter, it runs. We can change the > to something by modifying this var
PS3 - mostly used in ksh, this is used to define the prompt string displayed by 'select' command, which creates a menu, so in the menu, the prompt string for each option can be defined by changing this one
PS4- we can debug a shell script by putting a set -x while running it, this enables to print each command and then its results a + sign is put before each command, this "+" prompt string can be changed by defining the PS4 var.


Moderator's Comments:
Mod Comment Removed Self Promoting Links
# 3  
Old 03-05-2010
The $PS1 is used to display the user and host information
For example: thillaiselvan@webserver
\u - Username
\h - Hostname
\w - Full pathname of current directory.

$PS2 this is used to continuation of command.
Assume that you are typing a very lengthy commands that may be a sequence of some pipes.
So at this time we can broken this into multiple lines using \ character.
The default is '>'

If the command is not ended properly this will appear.

$PS3 this is the prompt for select inside a script

Example: if you are not setting this variable then your select script will appear like this
Code:
select i in mon tue wed exit
do
  case $i in
    mon) echo "Monday";;
    tue) echo "Tuesday";;
    wed) echo "Wednesday";;
    exit) exit;;
  esac
done

The output will look like this
1) mon
2) tue
3) wed
4) exit
#? 1
Monday
#? 4
[Note: This displays the default "#?" for select command prompt]
[/code]

If you are using the $PS3 then at this time this will be like this
Code:
PS3="Select a day (1-4): "
select i in mon tue wed exit
do
  case $i in
    mon) echo "Monday";;
    tue) echo "Tuesday";;
    wed) echo "Wednesday";;
    exit) exit;;
  esac
done

At this time the output will be like this
Code:
1) mon
2) tue
3) wed
4) exit
Select a day (1-4): 1
Monday
Select a day (1-4): 4
[Note: This displays the modified "Select a day (1-4): "
       for select command prompt]

PS4 - Used by “set -x” to prefix tracing output
defines the prompt that gets displayed, when you execute a shell script in debug mode as shown below.

Shell script and output WITHOUT PS4:
Code:
cat ps4.sh

set -x
echo "PS4 demo script"
ls -l /etc/ | wc -l
du -sh ~

Output is as follows
Code:
++ echo 'PS4 demo script'
PS4 demo script
++ ls -l /etc/
++ wc -l
243
++ du -sh /home/thillaiselvan
123M     /home/thillaiselvan
[Note: This displays the default "++" while tracing the output using set -x]

If we are using the PS4:
Code:
cat ps4.sh

export PS4='$0.$LINENO+ '
set -x
echo "PS4 demo script"
ls -l /etc/ | wc -l
du -sh ~

Output of this script is as follows

Code:
../ps4.sh.3+ echo 'PS4 demo script'
PS4 demo script
../ps4.sh.4+ ls -l /etc/
../ps4.sh.4+ wc -l
243
../ps4.sh.5+ du -sh /home/thillaiselvan
123M     /home/thillaiselvan
[Note: This displays the modified "{script-name}.{line-number}+"
       while tracing the output using set -x]


Last edited by thillai_selvan; 03-08-2010 at 08:00 AM..
# 4  
Old 03-05-2010
Hi Nila and Selven,

Thank you very much for your prompt repy. Nila I didn't find any url in your reply.

Regards
Ramesh
# 5  
Old 03-05-2010
From man bash, similar in man ksh
Quote:
PS1 The value of this parameter is expanded (see PROMPTING below) and used as the primary prompt string. The default value is ``\s-\v\$ ''.
PS2 The value of this parameter is expanded as with PS1 and used as the secondary prompt string. The default is ``> ''.
PS3 The value of this parameter is used as the prompt for the select command (see SHELL GRAMMAR above).
PS4 The value of this parameter is expanded as with PS1 and the value is printed before each command bash displays during an execution trace. The first character of PS4 is replicated multiple times, as necessary, to indicate multiple levels of indirection. The default is ``+ ''.
# 6  
Old 03-05-2010
Please check now
# 7  
Old 03-05-2010
Hi Nila,

Now I have found the link in your reply.


Thank you very much.


Regards
Ramesh
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh PS4 variable assign to `date` output

Hi guys, Is there a way to assign curent time to PS4 variable in ksh. My goal is to have each line produced by 'set -x' command to have a time stamp. Here is my code: $cat test #!/usr/bin/ksh export PS4="`date` " set -x echo "TRACE LINE ONE" echo "I WILL SLEEP FOR 10 SEC" sleep 10... (2 Replies)
Discussion started by: aoussenko
2 Replies

2. Shell Programming and Scripting

Is is possible to pass multiple entries in PS3 select ?

PS3="Enter corresponding number and hit enter:" select DIR in `cat mylist` QUIT do if then echo "INVALID INPUT" else if ; then my commands ..... else break fi fi REPLY='' done The above will return something like below : Select from the list of... (4 Replies)
Discussion started by: ningy
4 Replies

3. Shell Programming and Scripting

PS3 select when wrong input given

Hi All I am using the below code to chose a file to view : PS3="Select file to view : " select FILE in `ls` QUIT do if ; then clear cat $FILE else break fi REPLY='' done Everything works fine as long as I am giving the correct choice . But when i give a... (4 Replies)
Discussion started by: ningy
4 Replies

4. Shell Programming and Scripting

PS3 and SELECT, is it possible to put a line break?

Hi all, Before I give up on using SELECT for my first attempt at creating a menu driven script, can anyone please advise if it is possible to include a line break for PS3, I've tried putting in a \n and it does not work. Tried for both bash and ksh and both gives the same result. Preference... (3 Replies)
Discussion started by: newbie_01
3 Replies

5. Debian

Lost Password to Debian on PS3. Need help or better documentation for recreating a live-cd

About a year and half or so ago me and a friend set up Debian on a PS3. For a while I could not boot into the ps3 but managed to get it running again. I was very excited to get back in a play on the Debian distro we installed until I found neither of us could remember the password he put on it. ... (3 Replies)
Discussion started by: Azrael
3 Replies

6. Shell Programming and Scripting

Regarding PS1,PS2,PS3,PS4 variables.

Hello frnds... I m asking a general question... thought I never faced and used this (PS3) variable... or may be i m not aware of this... I know very well about the unix prompt string variables PS1,PS2,PS4. but please tell me what is the use of PS3 and where it's used. I didnt find... (5 Replies)
Discussion started by: clx
5 Replies

7. UNIX for Dummies Questions & Answers

how can unset for "PS4"

Hi, how can unset PS="+". i tryed like this: $ unset PS4; but it's not unseting permanently. it's showing oagain when i was login. pls help me on this. (1 Reply)
Discussion started by: koti_rama
1 Replies

8. HP-UX

Hardware Change 715/33 715/100 HIL - PS2 - XServer didnt start ???

I Need help ! We changed a WS HP 715/33 the new is a HP 715/100 ! The old one supports a HIL keyboard and mouse. The "new" one PS2 keyboard. We changed only the hardware not the harddisk - now the Xserver didnīt start ! What have we to configure thatt the WS supports the PS2 keyboard under... (0 Replies)
Discussion started by: rbrandt1
0 Replies
Login or Register to Ask a Question