且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何打印当前的bash提示?

更新时间:2023-01-27 11:34:39

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

Bash 4.4+ solution using parameter transformation for a prompt string: 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参考手册页上 Shell参数扩展:

${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.

如果参数是@*,则将操作依次应用于每个位置参数,并且扩展是结果列表.如果parameter是下标为@*的数组变量,则将操作依次应用于该数组的每个成员,并且扩展为结果列表.

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.

(另请参阅 

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

Z Shell (zsh) can do this with ${(%%)PS1} or with its print builtin's -P flag:

[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}"
adkatz is dining at 11:49:01 using zsh 5.7.1
[adamhotep@tabasco ~]% print -P "$TEST_STRING"
adkatz is dining at 11:49:07 using zsh 5.7.1
[adamhotep@tabasco ~]% 

Zsh扩展和替换手册告诉我们:

参数扩展标志 .如果左括号后面紧跟着左括号,则直到匹配的右括号的字符串都将作为标志列表.在重复标志是有意义的情况下,重复不必是连续的.例如,(q%q%q)的含义与可读性更高的(%%qqq)相同.支持以下标志:

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:

% 提示(请参见提示扩展).如果两次给出此标志,则已满 迅速扩展结果字,具体取决于 PROMPT_PERCENTPROMPT_SUBSTPROMPT_BANG选项的设置.

%    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.

来自用于文档="http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html#index-print" rel ="nofollow noreferrer"> print :

From the Zsh Builtins documentation for print:

-P进行即时扩展(请参见提示扩展).与-f结合使用时,仅在内插参数中解析提示转义序列,而不在格式字符串中解析

-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.