How to stop bash prompt colours from escaping
You cannot have a function that outputs both text and color, in the bash prompt. Everything the function outputs gets taken literally. I suggest going about it a different way.
First off, what the prompt tries to do, is check if you're in a git tree, and if so, add a colored (branch) to the prompt. Instead of checking everytime the prompt is printed, you really only need to do that check whenever you change directory, so we can override the only three commands that can change directory to set the variable git_branch to the name of the current branch, if any.
cd() {
builtin cd "$@" || return
git_branch=$(git branch 2>/dev/null|sed -n '/^[*] /s///p') || true
}
pushd() {
builtin pushd "$@" || return
git_branch=$(git branch 2>/dev/null|sed -n '/^[*] /s///p') || true
}
popd() {
builtin popd "$@" || return
git_branch=$(git branch 2>/dev/null|sed -n '/^[*] /s///p') || true
}
Next up, check if stderr is a terminal, and set colors with tput
user_color= dir_color= git_color= reset=
if [[ -t 2 ]]; then
user_color=$(tput setaf 2; tput bold)
dir_color=$(tput setaf 4; tput bold)
reset=$(tput sgr0)
fi
And a function that outputs one of two colors based on whether there are changes or not
_git_color() {
[[ $git_branch && -t 2 ]] || return
if git diff --quiet >/dev/null 2>&1; then
tput setaf 1
else
tput setaf 3
fi
}
Now, putting the prompt together. This is a bit cumbersome as some variables should be expanded at assignment (now), while some should be expanded when the prompt is printed, so we need to alternate quotes, plus it gets rather long. Using an array for this should help keep the overview
prompt=(
"\[$user_color\]" '\u@\h' "\[$reset\]"
: "\[$dir_color\]" '\w' "\[$reset\]"
'${git_branch:+ (}'
'\[$(_git_color)\]' '$git_branch' "\[$reset\]"
'${git_branch:+)}'
'\$ '
)
I ommited the debian_chroot stuff here, I've never needed it myself, you can of course add that if you want. Finally, join the prompt array and clean up the variables we no longer need.
printf -v PS1 %s "${prompt[@]}"
unset user_color dir_color reset prompt