首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PS3和PS4提示变量的奇怪行为

PS3和PS4提示变量的奇怪行为
EN

Unix & Linux用户
提问于 2018-04-23 15:29:14
回答 2查看 531关注 0票数 2

我知道可以定制提示符(详见“6.9控制及时手册”中的Bash部分),我已经做了一段时间了,但最近我注意到了一些奇怪的行为。

考虑以下两种情况:

无转义序列

代码语言:javascript
复制
PS1='\$ '
PS2='> '
PS3='#? '
PS4='+ '

带转义序列的

代码语言:javascript
复制
PS1='\[\e[1;34m\]\$\[\e[0m\] '
PS2='\[\e[1;34m\]>\[\e[0m\] '
PS3='\[\e[1;34m\]#?\[\e[0m\] '
PS4='\[\e[1;34m\]+\[\e[0m\] '

因此,问题是:

  • PS3按原样打印,而不解释转义序列。
  • PS4甚至没有打印出来。

我很肯定他们以前曾经工作过,但由于我不经常使用,所以我不知道他们什么时候行为不端。

技术细节

  • OS: Ubuntu 16.04.4
  • Shell: Bash 4.3.48(1)-release
  • Terminal模拟器: GNOME终端3.18.3 (不过,它也发生在虚拟终端中)
  • 据我所知,自系统安装(2017-06-09)以来,还没有进行过任何Bash更新。
EN

回答 2

Unix & Linux用户

回答已采纳

发布于 2018-04-23 16:01:48

来自man bash

代码语言:javascript
复制
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

因此,无论出于何种原因,PS3的不扩展都是记录在案的行为。

至于PS4,您需要导出变量以使其在新的bash调用中可用。而且您需要显式地设置跟踪选项,-v不会打开它:

代码语言:javascript
复制
pse@Mithos:~/.tmp$ export PS4='uuuu: '
pse@Mithos:~/.tmp$ bash -c "set -x; echo foo"
uuuu: echo foo
foo
票数 3
EN

Unix & Linux用户

发布于 2018-04-23 16:01:56

来自bash手册:

代码语言:javascript
复制
   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 multiple times,  as  necessary,  to  indicate
          multiple levels of indirection.  The default is ``+ ''.

PS3的定义没有说明它是以与其他提示符字符串相同的方式展开的。您看到的行为与文档一致。

票数 2
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/439513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档