The Goal
I'm trying to colorize my bash prompt on Mac OS X with the git branch (where available).
What I've Tried
With my limited bash knowledge, I pieced together the following code from Google searches and other questions:
function parse_git_branch() {
        branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
        if [[ -z "$branch" ]]; then
                return
        fi
        if [[ -z "$(git status -s 2>/dev/null)" ]]; then
                color=$'\e[1;32m'
        else
                color=$'\e[1;31m'
        fi
        echo "\[$color\] (${branch}) "
}
PS1="\h:\W \u\$(parse_git_branch)\[\e[0m\]\$ "
The Problem
While the coloration works, the prompt contains some of the escape sequences from parse_git_branch.
leonidas:AYI jason\[\] (master) $
In addition, things like command history (up) and recursive search (ctrl+r) yield extra characters.
leonidas:AYI jason\[\] (master) $h)`re': git status
The Questions
- How can I fix the escaping with proper visible and non-visible characters.
 - Should I use 
tputinstead of these color codes for wider support? 
PS1="...$(parse_git_branch)...", it is evaluated precisely once, while sourcing the file. You needPS1='...'to have it evaluated every time.