问题描述
如果我以前定义过这样的颜色变量:
If I have previously defined color variable like this:
txtred='\e[1;31m'
我将如何在 heredoc 中使用它:
cat << EOM
[colorcode here] USAGE:
EOM
我的意思是我应该写些什么来代替 [这里的颜色代码]
来呈现这种用法文字为红色? $ {txtred}
不起作用,因为这就是我在整个过程中使用的bash脚本,位于 heredoc
I mean what should I write in place of [colorcode here]
to render that USAGEtext red? ${txtred}
won't work, as that is what I am using throughout mybash script, outside of heredoc
推荐答案
您需要一些东西来解释 cat
不会执行的转义序列.这就是为什么需要 echo -e
而不是仅仅使 echo
正常运行的原因.
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
有效
,但是您也不能通过使用 textred = $(tput setaf 1)
使用转义序列,然后直接使用变量.
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
这篇关于着色heredocs,bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!