23
8
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?
Sorry to resurrect an answer, but what's the equivalent on dash/ash/sh? – Hosh Sadiq – 2018-06-07T20:55:19.600
@Hosh If they use readline,
– wjandrea – 2019-04-12T04:16:32.970\001
and\002
will work. Otherwise I'm not sure. Dash for example definitely doesn't use readline.That does it!
echo -e "\001\e[31m\002RED"
works as expected. Thanks! – MidnightLightning – 2011-06-23T20:38:19.887