Search code examples
zshzshrc

How do I include a variable in the prompt that changes if the variable changes (in zsh)


I'm trying to include the AWS_PROFILE variable in my zsh prompt so that I know which profile I'm currently working in. The problem is that when the value changes the prompt never updates in the same window. Here's my PS1 value:

PS1="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m-%B$AWS_PROFILE%b-%{$fg[yellow]%}%~ %{$reset_color%}%% "

The AWS_PROFILE value stays the same in that same window when I export it with a new value.

Any ideas?


Solution

  • You need to defer the parameter expansion to the time the prompt is evaluated. This means that you need to escape the $ in front of the parameters.

    That is, instead of $fg, you need a \$fg.