bash - 如何打印当前的 bash 提示符?

标签 bash prompt ps1

问题很简单。我想在我的 bash 脚本中评估 PS1 的当前值。

Google 上的所有资料都指向有关如何改进它的教程,但我想评估一下我当前的终端或至少 一些 终端将如何呈现它。

是否有任何软件/功能可以帮助我实现这一目标?当然,我希望对所有转义字符进行求值,因此 echo $PS1 在我的情况下没那么有用。

最佳答案

Bash 4.4+ 解决方案 对提示字符串使用参数转换:echo "${PS1@P}"

[adamhotep@tabasco ~]$ echo "the prompt is '${PS1@P}'"
the prompt is '[adamhotep@tabasco ~]$'
[adamhotep@tabasco ~]$ TEST_STRING='\u is dining at \t using \s \V'
[adamhotep@tabasco ~]$ echo "${TEST_STRING}"
\u is dining at \t using \s \V
[adamhotep@tabasco ~]$ echo "${TEST_STRING@P}"
adamhotep is dining at 21:45:10 using bash 5.0.3
[adamhotep@tabasco ~]$ 

来自Bash Reference Manual页面 Shell Parameter Expansion :

${parameter@operator}

Parameter transformation. The expansion is either a transformation of the value of parameter or information about parameter itself, depending on the value of operator.
Each operator is a single letter:

Q  The expansion is a string that is the value of parameter quoted in a
   format that can be reused as input.
E  The expansion is a string that is the value of parameter with backslash
   escape sequences expanded as with the $'…' quoting mechanism.
P  The expansion is a string that is the result of expanding the value of
   parameter as if it were a prompt string (see PROMPTING below).
A  The expansion is a string in the form of an assignment statement or
   declare command that, if evaluated, will recreate parameter with its
   attributes and value.
a  The expansion is a string consisting of flag values representing
   parameter's attributes.

If parameter is @ or *, the operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the operation is applied to each member of the array in turn, and the expansion is the resultant list.

(另请参阅重复问题 this answer 中的 Echo expanded PS1。)

Z Shell (zsh) 可以使用 ${(%%%)PS1} 或其 print 内置的 -P 标志:

[adamhotep@tabasco ~]% echo "the prompt is '${(%%)PS1}'"
the prompt is '[adamhotep@tabasco ~]%'
[adamhotep@tabasco ~]% print -P "the prompt is '$PS1'"
the prompt is '[adamhotep@tabasco ~]%'
[adamhotep@tabasco ~]% TEST_STRING="%n is dining at %* using %N $ZSH_VERSION"
[adamhotep@tabasco ~]% echo "$TEST_STRING"
%n is dining at %* using %N 5.7.1
[adamhotep@tabasco ~]% echo "${(%%)TEST_STRING}"
adamhotep is dining at 11:49:01 using zsh 5.7.1
[adamhotep@tabasco ~]% print -P "$TEST_STRING"
adamhotep is dining at 11:49:07 using zsh 5.7.1
[adamhotep@tabasco ~]% 

Zsh Expansion and Subsitution手册告诉我们:

Parameter Expansion Flags. If the opening brace is directly followed by an opening parenthesis, the string up to the matching closing parenthesis will be taken as a list of flags. In cases where repeating a flag is meaningful, the repetitions need not be consecutive; for example, (q%q%q) means the same thing as the more readable (%%qqq). The following flags are supported:

%    Expand all % escapes in the resulting words in the same way as in prompts (see Prompt Expansion). If this flag is given twice, full prompt expansion is done on the resulting words, depending on the setting of the PROMPT_PERCENT, PROMPT_SUBST and PROMPT_BANG options.

来自Zsh Builtins print 的文档:

-P    Perform prompt expansion (see Prompt Expansion). In combination with -f, prompt escape sequences are parsed only within interpolated arguments, not within the format string.

关于bash - 如何打印当前的 bash 提示符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22322879/

相关文章:

Bash 4.3.33 中的字符串替换(小写)- 错误替换错误

java - Windows中执行计划任务时如何隐藏svchost.exe(dos提示符)

linux - 修剪 PS1 的工作目录路径

bash - 如何缩短我的命令行提示当前目录?

bash - 为什么从 bash 脚本执行时 grep 会丢失彩色输出?

bash - 是否可以在使用原始脚本的同时运行重复的 bash 脚本?

javascript - 使用 JavaScript 中的提示方法进行故障排除

javascript - 如何测试条件语句提示的输入类型 - Javascript

git - 为什么我的 bash 提示不更新?

bash - 如何配置对话框 bash 的取消按钮?