I want to set a sub portion of my PS1 prompt string to a specific color/style that is chosen by passing into a bash function.
It is easy for me to explicitly put the color in the PS1 variable directly. However, when I to do this using a bash variable like $1
or like $MY_COLOR
instead of getting a colored part of the prompt, I end up with the exact characters from the PS1 color code as part of the prompt itself.
Here is my example:
function set_git_branch_prompt()
{
git_prompt_style="\\[\\e[1;35m\\]" # Default bold magenta
if [[ $# != 0 ]]; then
git_prompt_style="\\[\\e[$1m\\]"
fi
export PS1=$(echo "$PS1" | sed -r 's/(.*)\\n\\\$/\1$git_prompt_style\$(parse_git_branch)\\[\\e[00m\\]\\n\\\$/g')
}
Instead of the of the specified color (such as bold magenta) I end up with the characters such as \[\e[1;35m\]
as part of the prompt.
How do I go about having PS1 interprete $git_prompt_style as the actual PS1 code and not explicit characters?