- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题很简单。我想在我的 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 inprompts (see Prompt Expansion). If this flag is given twice, fullprompt expansion is done on the resulting words, depending on thesetting of thePROMPT_PERCENT
,PROMPT_SUBST
andPROMPT_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/
有什么方法可以自定义javascript提示框吗? 我想让我的提示只接受数字输入...(我想绑定(bind) keyup 事件 - 具体来说) 这可能吗? 最佳答案 默认的 javascript 提示
对我来说,现在是 bash-prompt-fiddling-time(总有一天……)。 我正在尝试获取两行提示: 第一行包含位置信息,背景颜色到行尾 第 2 行包含时间和上一个命令的退出代码 我快到了
大家好,我只是在学习 javascript,不过我有一些 html 和 css 背景。 我不知道如何只允许在提示中输入数字值,或者检查它是否是数字,如果不是则重新提示。文本框也是如此。 最佳答案 这是
编辑:原标题说“bash prompt”,但我使用“zsh”。我接受了@Artur R. Czechowski 的回答,因为它在 bash 上下文中是正确的,并且我能够在他的帮助下完成我的工作。 PR
是否可以配置stack ghci类似于ghci的方式通过 ~/.ghci 配置文件? 目前stack ghci不接受 ~/.ghci 中的设置文件。 我正在研究将提示设置为 lambda 而不是加载的
我喜欢在我的 PowerShell 提示符中包含 Mercurial/Git 存储库状态。有时这会减慢 prompt()功能相当大,导致长时间等待,直到您可以开始在 shell 中输入。我想知道,是否
我正在编写一个用于与远程服务交互的 PowerShell 模块。连接到远程服务时(通过模块中的函数),我想在提示符前添加用户名。断开连接后,我想删除用户名。 我以为我可以通过复制全局 prompt 来
我用java开发了我的远程屏幕共享软件。它工作正常,但是当使用 UAC 时,屏幕上会出现允许/禁止提示,但远程屏幕看不到它。所以每次用户都必须手动允许它。我想知道如何摆脱这个问题。 最佳答案 这正是
我有一个初始化脚本,它设置了几个变量供以后使用。最后一行定义了一个“提示”函数,我希望它能改变我的提示,作为初始化已经完成的指示。变量已设置,因此它不作为子进程运行,但我的提示顽固地停留在“PS”。出
我有一个与此类似的模板提示: prompt = f""" Write a poem about the topic delimited by triple backticks if it starts
我有一个与此类似的模板提示: prompt = f""" Write a poem about the topic delimited by triple backticks if it starts
我需要一些帮助才能恢复提示,现在我正在执行此 block ,不要让我恢复提示,我在运行末尾使用 &将此命令作为后台任务。执行脚本后如何返回提示符。 #!/bin/sh sudo su - user <
如果我的 html 中有一个简单的按钮: Start Game 我想要在我的 JS 中提示: var userAdjective = prompt("Please provide an Adjecti
我想提示用户在网页上进行一些输入。 我无法使用内置的 Javascript prompt 函数,因为它会在 IE 中创建安全警告。 是否有人们用来重新创建此内容的最佳实践/方法/示例? Javascr
我想将我的 bash 提示设置为固定宽度,并弥补 $ 之前的空格差异,因此无论长短,我的提示都保持相同的宽度: [name@host] ~/Directory/Dir...Another/LastDi
我有一个脚本可以读取用户的输入。这是我的代码: if [ -z $volreadexists ]; then echo -e "\tThis will overwrite the en
我的 ZSH 主题使用 .zsh-theme 文件中的 RPROMPT='$(vbox_status)' 输出我的 Vagrant/VBox VM 的状态(其中 vbox_status调用 a scr
我经常使用 vim,并且经常发现使用 !bash 进入命令行很有用。但是,我需要键入 exit 才能返回到 vim,有时我不确定我是否在子 shell 中或者这是否会关闭我的 session 。 我真
问题很简单。我想在我的 bash 脚本中评估 PS1 的当前值。 Google 上的所有资料都指向有关如何改进它的教程,但我想评估一下我当前的终端或至少 一些 终端将如何呈现它。 是否有任何软件/功能
如何在打印后台函数的输出后自动返回到我的 bash 提示符? 例如,当我在 bash shell 中运行以下脚本时: fn(){ sleep 10 echo "Done"
我是一名优秀的程序员,十分优秀!