在 Linux Deepin 的命令行中,我们有时会遇到 $PS1, $PS2, $PS3, $PS4 几个变量,他们分别代表什么意思呢?
在 Linux 中,一个好的习惯就是阅读 man pages,所以,我们先来查看一下 bash 的在线文档:
Default
$ man bash
1
$manbash
我们使用 /PS1 搜索相关条目,可找到下面的几行内容:
Default
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 mul‐
tiple times, as necessary, to indicate multiple levels of indi‐
rection. The default is ``+ ''.
1
2
3
4
5
6
7
8
9
10
11
12
PS1Thevalueofthisparameterisexpanded(seePROMPTINGbelow)
andusedastheprimarypromptstring.Thedefaultvalueis
``\s-\v\$''.
PS2ThevalueofthisparameterisexpandedaswithPS1andusedas
thesecondarypromptstring.Thedefaultis``>''.
PS3Thevalueofthisparameterisusedasthepromptfortheselect
command(seeSHELLGRAMMARabove).
PS4ThevalueofthisparameterisexpandedaswithPS1andthe
valueisprintedbeforeeachcommandbashdisplaysduringan
executiontrace.ThefirstcharacterofPS4isreplicatedmul‐
tipletimes,asnecessary,toindicatemultiplelevelsofindi‐
rection.Thedefaultis``+''.
PS1 变量的展开值用作主提示符字符串。其默认值为 "\s-\v\$ "。在 Linux Deepin 中其默认值为 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '。我们曾在《在 Linux Deepin 下设置类 Gentoo 的彩色 Bash 提示符》一文中讲述过 PS1 变量的简单设置,如需要更多信息,可查看其 man pages 的 PROMPTING 一节及《Bash Prompt HOWTO》一文。
PS2 变量展开的方式与 PS1 相同,其展开值用作次提示符字符串。用于提示接续你尚未完成输入的命令。其默认值为"> ",也可以自定义,如:
Default
$ export PS2='Secondary: '
$ for i in *
Secondary: do
Secondary: echo $i
Secondary: done
apple.txt
test
tmp.txt
1
2
3
4
5
6
7
8
$exportPS2='Secondary: '
$foriin*
Secondary:do
Secondary:echo$i
Secondary:done
apple.txt
test
tmp.txt
PS3 变量的值用于用作 select 命令的提示符。假设我们有一个名为 seasons 的脚本,其内容为:
Default
#! /usr/bin/env bash
echo "Which season do you like best?"
select i in spring summer autumn winter
do
case $i in
spring) echo "Spring";;
summer) echo "Summer";;
autumn) echo "Autumn";;
winter) echo "Winter";;
esac
done
1
2
3
4
5
6
7
8
9
10
11
#! /usr/bin/env bash
echo"Which season do you like best?"
selectiinspringsummerautumnwinter
do
case$iin
spring)echo"Spring";;
summer)echo"Summer";;
autumn)echo"Autumn";;
winter)echo"Winter";;
esac
done
其输出会像下面这样:
Default
Which season do you like best?
1) spring
2) summer
3) autumn
4) winter
#? 1
Spring
#? 2
1
2
3
4
5
6
7
8
Whichseasondoyoulikebest?
1)spring
2)summer
3)autumn
4)winter
#? 1
Spring
#? 2
这次,我们修改脚本的第二行,定义 PS3 变量:
Default
PS3=“Which season do you like best?”
1
PS3=“Whichseasondoyoulikebest?”
这次脚本的输出会变为:
Default
1) spring
2) summer
3) autumn
4) winter
Which season do you like best?1
Spring
Which season do you like best?
1
2
3
4
5
6
7
1)spring
2)summer
3)autumn
4)winter
Whichseasondoyoulikebest?1
Spring
Whichseasondoyoulikebest?
PS4 变量展开的方式与 PS1 相同。在执行跟踪时该展开值都会显示在每一条命令的前面。PS4 的值的第一个字符会按需重复多次,用于表示多个间接层次。其默认值为 "+ "。现在我们使用 bash -x ./seasons 再次执行上面的脚本,或将该脚本的标头修改为下面的内容:
Default
#! /bin/bash -x
1
#! /bin/bash -x
或
Default
#! /usr/bin/env bash
set -x
1
2
#! /usr/bin/env bash
set-x
我们会发现,默认状况下会在命令前显示 +x 这个提示符。