Chapter 5: PS1, PS2, PS3, PS4 and PROMPT_COMMAND __Linux 101 Hacks

Chapter 5: PS1, PS2, PS3, PS4 andPROMPT_COMMAND

        __Linux 101 Hacks

Hack 29. PS1 - Default Interaction Prompt

 

  
    
- bash - 3.2 $ export PS1 = " \u@\h \w> "
ramesh@dev
- db ~> cd / etc / mail
ramesh@dev
- db / etc / mail >

[Note: Prompt changed to
" username@hostname current-dir> " format]

 

    \u – Username

 

    \h – Hostname

    \w - Full pathname of current directory. Please note that when you are in the home directory, this will display only ~ as shown above.

Note that there is a space at the end in the value of PS1.

Make this setting permanent by adding export PS1=”\u@\h \w> ” to either .bash_profile (or) .bashrc.

 

Hack 30. PS2 - Continuation Interactive Prompt

A very long command can be broken down to multiple lines by giving \ at the end of the line. The default interactive prompt for a multi-line command is “> “. 

Let us change this default behavior to display “continue->” by using PS2 environment variable as shown below.

 

  
    
ramesh@dev - db ~> export PS2 = " continue-> "

 

 

 

Hack 31. PS3 - Prompt used by “select” inside shell script

    Shell script and output WITHOUT PS3:   

  
    
1 select i in mon tue wed exit
2 do
3 case $i in
4 mon) echo " Monday " ;;
5 tue) echo " Tuesday " ;;
6 wed) echo " Wednesday " ;;
7 exit) exit;;
8 esac
9 done
10  

 

 

    Shell script and output WITH PS3:

 

代码
   
     
1 PS3 = " Select a day (1-4): "
2 select i in mon tue wed exit
3 do
4 case $i in
5 mon) echo " Monday " ;;
6 tue) echo " Tuesday " ;;
7 wed) echo " Wednesday " ;;
8 exit) exit;;
9 esac
10 done
11  

 

Hack 32. PS4 - Used by “set -x” to prefix tracing output

The PS4 shell variable defines the prompt that gets displayed, when you execute a shell script in debug mode as shown below.

    Shell script and output WITHOUT PS4:

代码
   
     
1 ramesh@dev - db ~> cat ps4.sh
2 set - x
3 echo " PS4 demo script "
4 ls - l / etc / | wc - l
5 du - sh ~
6
7 ramesh@dev - db ~> . / ps4.sh
8   ++ echo ' PS4 demo script '
9 PS4 demo script
10   ++ ls - l / etc /
11   ++ wc - l
12   243
13   ++ du - sh / home / ramesh
14 48K / home / ramesh
15  

 

[Note: This displays the default "++" while tracing the output using set -x]

    Shell script and output WITH PS4:

代码
   
     
1 ramesh@dev - db ~> cat ps4.sh
2 export PS4 = ' $0.$LINENO+ '
3 set - x
4 echo " PS4 demo script "
5 ls - l / etc / | wc - l
6 du - sh ~
7
8 ramesh@dev - db ~> . / ps4.sh
9 .. / ps4.sh. 3 + echo ' PS4 demo script '
10 PS4 demo script
11 .. / ps4.sh. 4 + ls - l / etc /
12 .. / ps4.sh. 4 + wc - l
13   243
14 .. / ps4.sh. 5 + du - sh / home / ramesh
15 48K / home / ramesh
16  

 

[Note: This displays the modified "{script-name}.{line-number}+" while tracing the output using set -x]

 

 

Hack 33. PROMPT_COMMAND

Bash shell executes the content of the PROMPT_COMMAND just before displaying the PS1 variable.

  
    
ramesh@dev - db ~> export PROMPT_COMMAND = " date +%H:%M:%S "
22 :0 8 : 42

ramesh@dev
- db ~> export PROMPT_COMMAND = " echo -n [$(date +%H:%M:%S)] "
[
22 :0 8 : 51 ]

 

 

你可能感兴趣的:(command)