Conditional statement in a function for PS1 are not re-evaluated

export PS1=$(displayPS1);

This will run displayPS1, and the if statements within once, assigning the result to the prompt. The conditions won't be processed again after that.

Instead, put the function call in PROMPT_COMMAND, so it gets called every time the prompt is going to be printed. So either

PROMPT_COMMAND='PS1=$(displayPS1)'

or perhaps rather

PROMPT_COMMAND=setPS1

and make setPS1 a function that sets PS1 itself. (Getting rid of the command substitution saves a fork from the subshell invocation every time the prompt is changed.)


Use the quotes.

PS1='$(displayPS1)'

If you don't then the function is evaluated at assignment time.

Tags:

Bash

Prompt