Question:
I’m trying to add color to a part of my Ubuntu bash prompt. This part isn’t working: (\e[0;31m\1\e[m) It works, however, if I place it within the PS1 export.What am I doing wrong?
Answer:
Let’s imagine you asked this question:- Why does putting
\e\[0;31m
inPS1
variable content result in a colored text output, when putting the same string insidesed
s
command replacement expression does not result in colored output?
It’s because Bash when printing the content of
PS1
variable changes the sequence of two characters \
and e
by the byte with the value 033 octal or 0x1b hexadecimal. In contrast, for sed
the sequence of \
e
characters as part of replacement expression is technically invalid and in practice is not interpreted specially and with GNU sed
just results in a literal e
character on output.If you want to output the hex byte 0x1b with GNU
sed
you can use the \x1b
escape sequence, or you can rely on Bash ANSI-C quoting to pass the byte literally to sed.If you have better answer, please add a comment about this, thank you!