什么 shell 实现了变量 PS1 的 POSIX 语义?
What shell implements POSIX semantics for the variable PS1?
在IEEE Std 1003.1, 2004 edition's sh的2.5.3节中,关于PS1
环境变量,定义为:
Each time an interactive shell is ready to read a command, the value
of this variable shall be subjected to parameter expansion and written
to standard error. The default value shall be "$ ". For users who have
specific additional implementation-defined privileges, the default may
be another, implementation-defined value. The shell shall replace each
instance of the character '!' in PS1 with the history file number of
the next command to be typed. Escaping the '!' with another '!' (that
is, "!!" ) shall place the literal character '!' in the prompt. This
volume of IEEE Std 1003.1-2001 specifies the effects of the variable
only for systems supporting the User Portability Utilities option.
所以似乎可以在PS1变量中使用!
来替换为下一个要键入的命令的历史文件编号。如果您在 dash
等 Bourne shell 实现中尝试这样做,您会发现它没有实现。它也没有在今天的 GNU bash 中实现。
哪个 shell 实现了它?
如果您设置 POSIXLY_CORRECT
环境变量或使用 --posix
选项调用 bash,bash 会实现此功能:
PS1='!$ ' POSIXLY_CORRECT=1 bash
2$ echo foo
foo
3$ !2
echo foo
foo
3$
在IEEE Std 1003.1, 2004 edition's sh的2.5.3节中,关于PS1
环境变量,定义为:
Each time an interactive shell is ready to read a command, the value of this variable shall be subjected to parameter expansion and written to standard error. The default value shall be "$ ". For users who have specific additional implementation-defined privileges, the default may be another, implementation-defined value. The shell shall replace each instance of the character '!' in PS1 with the history file number of the next command to be typed. Escaping the '!' with another '!' (that is, "!!" ) shall place the literal character '!' in the prompt. This volume of IEEE Std 1003.1-2001 specifies the effects of the variable only for systems supporting the User Portability Utilities option.
所以似乎可以在PS1变量中使用!
来替换为下一个要键入的命令的历史文件编号。如果您在 dash
等 Bourne shell 实现中尝试这样做,您会发现它没有实现。它也没有在今天的 GNU bash 中实现。
哪个 shell 实现了它?
POSIXLY_CORRECT
环境变量或使用 --posix
选项调用 bash,bash 会实现此功能:
PS1='!$ ' POSIXLY_CORRECT=1 bash
2$ echo foo
foo
3$ !2
echo foo
foo
3$