Home » 2014 » September » 21 » How to define command prompt strings in Linux : what are $PS1, $PS2, $PS3 and $PS4?

7:30 AM
How to define command prompt strings in Linux : what are $PS1, $PS2, $PS3 and $PS4?

Share on Google+

Here in this article, we will learn how to change your command prompt strings in Linux environment or keep a prompt string of your own choice.

Before we change the command prompt strings, we need to learn the global variables $PS1, $PS2, $PS3, $PS4. Because these are the variable which defines the prompt strings. So lets have a look at short description on them.


  • PS1: This is the global variable used to define the main prompt string in linux environment when we login to a terminal. To check the default PS1 value, use "echo $PS1".

[shanky@HYDBMW ~]$ echo $PS1
[\u@\h \W]\$

shanky@localhost:/home/shanky:> echo $PS1
${USER}@${HOST}:${PWD}:>

So here, first prompt says PS1 as [\u@\h \W]\$. u, h and w or W are acronnyms which stands for username, hostname, present working directory respectively.

  • PS2 : This is another variable which sets or defines the prompt string of a subshell. Suppose you have entered a command incomplete, the shell goes into a subshell where it asks for remaining part of the command. And, when you provide rest of the command, the command is executed. Usually PS2 value is ">".

[shanky@HYDBMW ~]$ echo $PS2
>
Example: cat >> afile

>

>

  • PS3: This variable is used in ksh scripts in 'select' commands mostly. By default, it is a blank line. It defines the prompt string displayed by 'select' command.

[shanky@HYDBMW ~]$ echo $PS3

[shanky@HYDBMW ~]$

  • PS4: This is the last variable of prompt strings which is used in debugging of a shell script. When we enable "set -x" in a shell script, the script is executed command by command and ech command is preceded by the value of this variable. Usually it is "+".

[shanky@HYDBMW ~]$ echo $PS4
+
[shanky@HYDBMW ~]$

shanky@localhost:/home/shanky/test:> casetest.ksh two
+ urchoice=two
+ items=( cofee tea limejuice banana-sake )
+ print 'The choice is two'
The choice is two
+ print 'You get tea!!'
You get tea!!


Ok, so now we have a brief idea about  prompt string variuables. Lets try to change the prompt string variables.

  • We can change the PS1 value using export command to some other prompt string as per our own choice.

[shanky@HYDBMW ~]$ export PS1='shankar@shankysportal # '
shankar@shankysportal # echo $PS1
shankar@shankysportal #
shankar@shankysportal #

shankar@shankysportal # export PS1='shankar@$HOSTNAME $'
[email protected] $echo $PS1
shankar@$HOSTNAME $
[email protected] $
 

  • You may notice that the value of PS1 has changed and the command prompt of the main shell is also changed.
  • In the similar fashion you can change the values of other prompt string variables and hence the prompt strings.
export PS*=<new value of prompt string>

  • But this variables are effecttive only in the current session. if you exit form the session and login again, it will again revert to previous values.
  • If you want them to be persistent, you need to add them into your profile file. It is recommeded that one should make changes in his own profile file only and not in the systme profile file (/etc/profile).
  • Have a look at my bash profile file. I have added the export statement here.

shanky@shankysportal $ tail .bash_profile
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
export
PS1='$USER@shankysportal $ '

  • Once you have done this, reload your profile file or logout and login again. To reload, use ". .bash_profile
  • Now your PS1 value is changed permanently!!

 

If like this article, please share!!

 
 

Category: Open System-Linux | Views: 1332 | Added by: shanky | Tags: PS4, ps2, set prompt string in linux, export, how to change prompt string in linu, PS1, PS3 | Rating: 0.0/0

Related blogs


You may also like to see:


[2017-08-13][Open System-Linux]
Perl trick to remove Ctrl - M charater from a file
[2014-11-07][Open System-Linux]
FIND utility in Linux to search for files or directories.
[2014-01-08][Open System-Linux]
SSH Login Setup
[2015-06-01][Open System-Linux]
DIG command : A DNS lookup utility
[2015-09-09][Open System-Linux]
Some funny and cool commands in Linux

Total comments: 0
ComForm">
avatar