Monday, April 1, 2013

Linux/Unix Shell : PS2 prompt examples

PS2(Prompt String 2) is one of the prompts available in Linux/Unix. The other prompts are PS1, PS3 and PS4. This is very much useful for entering a large command in multiple lines and when you execute incomplete command, this prompt will come into picture.

Check what your default PS2 prompt by executing below command:

echo $PS2


Example1: We can change this prompt to different one with a bit meaning such as "continue your command here" etc as shown in below example.

PS2='Continue here..! '

Now start executing incomplete command as below.

echo "How are you 
Continue here..! and what you do
Continue here..!ok lets end it here.."

How are you
and what you do
ok lets end it here..

If you see the prompt changed from '>' to ''Continue here..!'. this will be very handy and more informative when dealing with a command which spreads on multiple lines.
Example2: Below prompt will give you more information when typing data as your log in name and server where you log in.

PS2='\u@\h ::  '  

Set the above PS2 prompt and check it your self with following data
Example output:

[ ~]$ PS2='\u@\h :: '
[ ~]$ echo "This is how :: it works :: buddy"
This is how
it works

Other special characters like \u and \h includes below strings

\d the date in "Weekday Month Date" format (e.g., "Tue May 26")
\e an ASCII escape character (033)
\h the hostname up to the first .
\H the full hostname
\j the number of jobs currently run in background
\l the basename of the shells terminal device name
\n newline
\r carriage return
\s the name of the shell, the basename of $0 (the portion following the final slash)
\t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format
\@ the current time in 12-hour am/pm format
\A the current time in 24-hour HH:MM format
\u the username of the current user
\v the version of bash (e.g., 4.00)
\V the release of bash, version + patch level (e.g., 4.00.0)
\w Complete path of current working directory
\W the 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 $
\nnn the 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

Explore above special characters and use them whichever suits best for you.

Example3: Setting the line number to the command you are executing

[ ~]$PS2='${LINENO} ::'
[ ~]$echo "How are you
25 ::mand
26 ::"
How are you

Combining above two example you will get a good combination

[ ~]$ PS2='\u@\h :: ${LINENO} ::'
[ ~]$ echo "how are you :: 29 ::buddy :: 30 ::this is what :: 31 ::I am saying"
how are you
this is what
I am saying

Make this prompt(PS2) permanent by editing ~/.bashrc file for a BASH Shell. In our next post we will see how to use PS3 prompt in Shell scripts.


Post a Comment