1,405 瀏覽數

Linux Bash Shell: PS3

Linux Bash Shell 下的 PS1,PS2,PS3,PS4 有特別定義.下面來看看.

[root@localhost ~]# echo $PS1
[\u@\h \W]\$
[root@localhost ~]# echo $PS2
>
[root@localhost ~]# echo $PS3

[root@localhost ~]# echo $PS4
+

PS1

PS1 就是我們看到的提示字元,參數說明如下.

[root@localhost ~]# echo $PS1
[\u@\h \W]\$
  • \u – Username
  • \h – Hostname
  • \w – Full pathname of current directory.

可以直接在 ~/.bash_profile 或是 ~/.bashrc 設定 PS1 登出或是重新開機就會生效.

[root@localhost ~]# vi .bash_profile
...
export PS1="[\u@\h \W]\$ "

更多 PS1 參數設定請參考 http://www.thegeekstuff.com/2008/09/bash-shell-ps1-10-examples-to-make-your-linux-prompt-like-angelina-jolie/

PS3

PS3 是使用在 bash script file 的 case.可以看到下面了的範例,一個有定義 PS3 ,讓原本 #? 的提示字元變成 PS3 定義的 Select a day (1-4):

[root@localhost ~]# vi ps3.sh
select i in mon tue wed exit
do
  case $i in
    mon) echo "Monday";;
    tue) echo "Tuesday";;
    wed) echo "Wednesday";;
    exit) exit;;
  esac
done
[root@localhost ~]# chmod a+x ps3.sh 
[root@localhost ~]# ./ps3.sh
1) mon
2) tue
3) wed
4) exit
#? 1
Monday
#? 4
[root@localhost ~]# vi ps3.sh
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

[root@localhost ~]#  ./ps3.sh
1) mon
2) tue
3) wed
4) exit
Select a day (1-4): 1
Monday
Select a day (1-4): 4

參考文章 http://www.thegeekstuff.com/2008/09/bash-shell-take-control-of-ps1-ps2-ps3-ps4-and-prompt_command

發表迴響