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?