问题很简单.我想评估PS1我的bash脚本中的当前值.
谷歌上的所有材料都指向了拉皮条的教程,但我想评估一下它将如何由我当前的终端呈现,或者至少由某个终端呈现.
是否有任何软/功能可以帮助我实现这一目标?当然我想要评估所有转义字符,所以echo $PS1在我的情况下这不是很有用.
我会这样得到它:
echo $PS1
Run Code Online (Sandbox Code Playgroud)
然后用编辑器编辑它。之后进行测试(这是在会话处于活动状态时设置的):
PS1='\[\033[1m\]\[\033[34m\]\u\[\033[90m\]@\[\033[01;35m\]\h:\[\033[01;32m\]\W\[\033[0m\]$ '
Run Code Online (Sandbox Code Playgroud)
(\u 表示用户,\h 表示主机,\w 表示完整路径,\W 表示短路径)
如果我喜欢它,我将通过更改~/.bashrc中PS1的值使其永久化
附:
如果你想查看所有全局变量:
printenv
Run Code Online (Sandbox Code Playgroud)
或者:
printenv <name_of_var_to_see>
Run Code Online (Sandbox Code Playgroud)
使用提示字符串的参数转换的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 ~]$
Run Code Online (Sandbox Code Playgroud)
${parameter@operator}参数转换。扩展是参数值的转换或有关参数本身的信息,具体取决于运算符的值。
每个运算符都是一个字母:Run Code Online (Sandbox Code Playgroud)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为
@或*,则将操作依次应用于每个位置参数,并且扩展为结果列表。如果parameter是下标为@或的数组变量*,则将操作依次应用于该数组的每个成员,并且扩展为结果列表。
(另请参阅重复问题Echo扩展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}"
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 ~]%
Run Code Online (Sandbox Code Playgroud)
该岩组扩展和Subsitution手册告诉我们:
参数扩展标志。如果左括号后面紧跟着右括号,则直到匹配的右括号的字符串都将作为标志列表。在重复标志是有意义的情况下,重复不必是连续的。例如,
(q%q%q)与更具可读性的含义相同(%%qqq)。支持以下标志:
…
%%以与提示中相同的方式 扩展所有生成的单词中的转义符(请参阅Prompt Expansion)。如果这个标志出现两次,充分提示扩张对得到的话做了,这取决于的设置PROMPT_PERCENT,PROMPT_SUBST和PROMPT_BANG选项。
从Zsh Builtins文档中获得print:
-P执行即时扩展(请参阅提示扩展)。与结合使用时-f,提示转义序列仅在插值参数中解析,而不在格式字符串中解析。
| 归档时间: |
|
| 查看次数: |
7787 次 |
| 最近记录: |