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

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

我知道可以自定义提示(如6.9 控制提示Bash 手册中的部分),我已经这样做了一段时间,但我最近注意到一些奇怪的行为。

考虑以下两种情况:

没有转义序列

PS1='\$ '
PS2='> '
PS3='#? '
PS4='+ '

没有转义序列

使用转义序列

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甚至没有打印。

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


技术细节

  • 操作系统:乌班图 16.04.4
  • 壳:Bash 4.3.48(1)-发布
  • 终端模拟器:GNOME Terminal 3.18.3(不过,它也发生在虚拟终端中)
  • 据我所知,自系统安装(2017-06-09)以来,没有任何 Bash 更新。

答案1

man bash

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而不打开它:

pse@Mithos:~/.tmp$ export PS4='uuuu: '
pse@Mithos:~/.tmp$ bash -c "set -x; echo foo"
uuuu: echo foo
foo

答案2

bash手册:

   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的定义不是声明它以与其他提示字符串相同的方式扩展。您看到的行为与文档一致。

相关内容