I really struggle understanding the behavior of echo
(and others such commands) while processing ANSI escape codes to print colored messages.
I know I can use the -e
option like this:
echo -e "\e[32m FOO \e[0m"
My message will be successfully colored.
Also, I noticed that I could assign the output of echo -e
to a variable, and then re-use this variable in a new echo
command without the -e
option, and this would work anyway.
foo=$(echo -e "\e[32m FOO \e[0m")
echo $foo
So, what are the actual "raw bytes" emitted by echo -e
when encountering ANSI codes? What does my foo
variable contain? How could I integrate them directly in my echo "??? FOO ???"
without needing the -e
option?
Esc
value, which is not printable, hence the-e
option which eases its usage using ascii\e
. – Delgan May 03 '19 at 12:48foo=$'\e[32mFOO\e[0m'
. – JdeBP May 03 '19 at 13:12