coloring in heredocs, bash

bash, colors, heredoc

Solution

You need something to interpret the escape sequence which `cat` won't do. This is why you need `echo -e` instead of just `echo` to make it work normally.

cat << EOM
$(echo -e "${txtred} USAGE:")
EOM

works

but you could also not use escape sequences by using `textred=$(tput setaf 1)` and then just use the variable directly.

textred=$(tput setaf 1)

cat <<EOM
${textred}USAGE:
EOM

Problem

If I have previously defined color variable like this: ``` txtred='\e[1;31m' ``` How would I use it in heredoc: ``` cat << EOM [colorcode here] USAGE: EOM ``` I mean what should I write in place of `[colorcode here]` to render that USAGE text red? `${txtred}` won't work, as that is what I am using throughout my bash script, outside of heredoc

Original source