I have this in my .bashrc:
LIGHTGREEN=\"\\[\\033[1;32m\\]\"
LIGHTRED=\"\\[\\033[1;31m\\]\"
WHITE=\"\\[\\033[0;37m\\]\"
RESET=\"\\[\\033[0;00m\\]\"
function
Use \001 instead of \[ and \002 instead of \], and be aware of the consequences of usingPROMPT_COMMAND as that method will reset the prompt every single time (which can also be just what you want).
The solution for bash prompt echoing colors inside a function is explained here:
The
\[\]are only special when you assign PS1, if you print them inside a function that runs when the prompt is displayed it doesn't work. In this case you need to use the bytes\001and\002
There is also this other answer that points in the same direction:
bash-specific
\[and\]are in fact translated to\001and\002
Setting PS1 inside a function called by PROMPT_COMMAND as suggested in the accepted aswer resets PS1 every single time not allowing other scripts to easily modify your promtp (for example Python virtualnenv activate.sh):
$ echo $PS1
$ PS1="(TEST)$PS1"
$ echo $PS1
<(TEST) is not prepended to PS1 if you are using PROMPT_COMMAND as it is reset>