linux 显示ps1变量值,[提示] Linux Deepin 中 $PS1, $PS2, $PS3, $PS4 等变量的解释

在 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 这个提示符。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值