小智 9
谷歌会为你找到答案。用红色打印 Hello world:
echo -e "\033[0;31mHello world\033[0m"
Run Code Online (Sandbox Code Playgroud)
解释
<esc><attr>;<foreground>m
<esc> = \033[ ANSI escape sequence, some environments will allow \e[ instead
<attr> = 0 Normal text - bold possible with 1;
<foreground> = 31 30 + 1 = Color red - obviously!
m = End of sequence
\033[0m Reset colors (otherwise following lines will be red too)
Run Code Online (Sandbox Code Playgroud)
查看http://en.wikipedia.org/wiki/ANSI_escape_code以获取颜色和其他功能(粗体等)的完整列表。
命令 tput(如果可用)将使生活更轻松:
echo -e "$(tput setaf 1)Hello world$(tput sgr0)"
Run Code Online (Sandbox Code Playgroud)
甚至可以将序列保存在 vars 中以方便使用。
ERR_OPEN=$(tput setaf 1)
ERR_CLOSE=$(tput sgr0)
echo -e "${ERR_OPEN}Hello world${ERR_CLOSE}"
Run Code Online (Sandbox Code Playgroud)