0

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

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

  • Please read [correct-bash-and-shell-script-variable-capitalization](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization) and then fix your variable names. – Ed Morton May 14 '23 at 15:32
  • Do you want `foo` to be tested once where `PS1` is declared or every time the value of `PS1` is printed? – Ed Morton May 14 '23 at 15:58

2 Answers2

4

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
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

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
 $
Philippe
  • 20,025
  • 2
  • 23
  • 32