更改文件名文本的颜色

ami*_*gal 7 linux shell colors bash filenames

我正在编写脚本来初始化和配置一个包含许多组件的大型系统。
每个组件都有自己的日志文件。

每当其安装/配置发生错误时,我想将组件文件名的颜色更改为红色。

我该怎么做?

小智 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)