zsh random color in PS1
zshrc
Solution
You can use a `precmd` hook which will be evaluated before each prompt:
randomise_prompt_colour () {
PS1="%n%B%F{$((RANDOM % 8))}@%m %~ %(!.#.\$) "
}
add-zsh-hook precmd randomise_prompt_colour
Problem
My `.bashrc` `PS1` (abridged) is ``` \u\[\e[01;3$(($RANDOM % 8))m\]@\h \w $' ``` With the way bash works, it interpolates the random color after each command so the `@` is a different color each time (at least in the 31-37 range). However, I've been unable to do something similar in zsh. The `$''` syntax does not allow for command substitution, and concatenating does not work either: ``` $'\e[01;3'$(($RANDOM % 8)) # The \e[01;3 character is printed first, then the random number ``` Using quotes `"` does not work either; it just prints out a literal `\e...` I know that zsh also has some built in text colors like `%{$fg[red]%}` and I could somehow select a random color from the array, but the problem is that it needs to be randomly selected by the `PS1` and not simply on start up or the random choice will be made only once. Is there any way I can interpolate a random number in the PS1 in zsh to achieve this?