I want to display the version of Node in my bash command prompt. I created a function to generate that portion of PS1. It gets the major version of Node, and colors it based on its numeric value. In my .bash_profile
:
nv_ps1 () {
nv_re="v([0-9]+)\.[0-9]+\.[0-9]"
if [[ $($NVM_BIN/node --version) =~ $nv_re ]]; then
nv_major=${BASH_REMATCH[1]}
let nv_color=$nv_major+26
echo "\[\e[${nv_color}m\]$nv_major\[\e[m\]"
fi
}
export PS1="[\$(nv_ps1) \[\e[36m\]\t\[\e[m\] \W]\\$ "
The escape sequences in the function output don't get evaluated, though, and the command prompt ends up looking like this:
[\[\e[34m\]8\[\e[m\] 08:54:58 ~]$
How can i get this to work?
PROMPT_COMMAND
to build the value ofPS1
rather than embedding commands in the prompt itself.