Bash提示符PS1的换行必须是有条件的。

huangapple go评论48阅读模式
英文:

Bash prompt PS1 newline must be conditional

问题

如何使 PS1 中的新行 \n 取决于先前的 Bash 变量,因为这不能成功:

    foo=`

     NEWLINE=
     #...
     ((foo)) && {
      THELINE=foobar
      NEWLINE='\n'
     }

PS1='$(echo $THELINE)$NEWLINE\w-$ '

如果 foo 不为 0,则 THELINE 是 "foobar" 并带有换行,否则无论 THELINE 是什么,都不应加入换行符。

真诚的有用帮助是宝贵的

`$.............``

英文:

How do we have PS1 newline \n being conditional on whether previous Bash variable's one, as this cannot succeed:

foo=

 NEWLINE=
 #...
 ((foo)) && {
  THELINE=foobar
  NEWLINE='\n'
 }

PS1='$(echo $THELINE)$NEWLINE\w-$ '

if foo not 0 then THELINE is "foobar" with a newline, otherwise whatever THELINE is it musn't be given \n

sincere useful help valuable

`$.............``

答案1

得分: 4

你可以使用 PROMPT_COMMAND 在每次提示前调用一个函数。

很抱歉,我不理解你试图做什么,所以无法准确地告诉你如何完成你想要的。不过以下内容可以帮到你:

prompt_cmd() {
    if [[ condition ]]; then
        PS1="true_prompt$ "
    else
        PS1="false_prompt$ "
    fi
}
export PROMPT_COMMAND=prompt_cmd
英文:

You can use PROMPT_COMMAND to make bash call a function before every prompt.

Unfortunately, I do not understand what you are trying to do so I can't show you exactly how to do what you want. This should get you started though:

prompt_cmd() {
    if [[ condition ]]; then
        PS1="true_prompt$ "
    else
        PS1="false_prompt$ "
    fi
}
export PROMPT_COMMAND=prompt_cmd

答案2

得分: 0

你可以直接将逻辑放在PS1中,如下所示:

PS1='$(((foo)) && echo "foobar\n\r" || echo "no") $ '

示例:

$ PS1='$(((foo)) && echo "foobar\n\r" || echo "no") $ '
no $ foo=1
foobar
 $
英文:

You can put the logic directly in PS1 as :

PS1='$(((foo)) && echo $"foobar\n\r" || echo "no") $ '

example:

$ PS1='$(((foo)) && echo $"foobar\n\r" || echo "no") $ '
no $ foo=1
foobar
 $

huangapple
  • 本文由 发表于 2023年5月14日 12:43:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定