Question: In a Bash Prompt (PS1 variable), I'm calling a function to potentially add text to the prompt: `export PS1="\u@\h \$(my_function) \$ "`

However, the function in the prompt contains ANSI color codes that change based on the output of the function (sometimes red, sometimes green). Adding "`\[`" to the PS1 variable should escape those codes as non-printing, but if I do an `echo` in the function, the "`\[`" get printed literally in the prompt.

How can I escape these ANSI color codes from within a function for use in a bash prompt?

Answer: 
Here's a nice complete answer. I had to do a lot more digging to figure out where the \001 etc. had to go. Hope this helps.

```
# Color prompt for git
reset=$(tput sgr0)
boldgreen=$(tput setaf 2)$(tput bold)
cyan=$(tput sgr0)$(tput setaf 6)
boldred=$(tput setaf 1)$(tput bold)
boldwhite=$(tput setaf 7)$(tput bold)
boldyellow=$(tput setaf 3)$(tput bold)

PARENCLR=$'\001\e[0;36m\002'
BRANCHCLR=$'\001\e[1;33m\002'

alias branchname="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/ ${PARENCLR}(${BRANCHCLR}\1${PARENCLR}\)/'"

GIT_STATUS='$(branchname)'

PROMPT_CHAR="\$"
PS1="\[$boldgreen\]\u\[$cyan\]::\[$boldred\]\h \[$cyan\]{\[$boldwhite\].../\W\[$cyan\]}\[$reset\]$GIT_STATUS\[$reset\]$PROMPT_CHAR "

```

The way I have it set up here, the git branch parentheses only appear if you're in a git branch, otherwise it's blank.
If you want to use them in the prompt, then you do need to do the `\[`. But if you want to use it in an echo, you have to use `\033[`.