在linux终端上编译一些项目时,我通常得到一个包含大量信息的长输出.通常这个信息是MONOCHROME.我想知道bash是否可以以某种方式进行修改,因此在所有输出或某些特定输出中(例如来自Makefile等)我可以获得不同的颜色依赖,例如:
make[1]: Leaving directory
Run Code Online (Sandbox Code Playgroud)
要么
g++ -DHAVE_CONFIG_H -I.
Run Code Online (Sandbox Code Playgroud)
等等
谢谢
Fab*_*bio 14
我发现在bash tput setf中不起作用.我发现这个命令对bash运行良好
handy tput commands
tput bold - Bold effect
tput rev - Display inverse colors
tput sgr0 - Reset everything
tput setaf {CODE}- Set foreground color, see color {CODE} below
tput setab {CODE}- Set background color, see color {CODE} below
Colors {code} code for tput command
Color {code} Color
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White
Run Code Online (Sandbox Code Playgroud)
Jef*_*den 10
当然,只需使用Bash函数,比如说这个:
make()
{
pathpat="(/[^/]*)+:[0-9]+"
ccred=$(echo -e "\033[0;31m")
ccyellow=$(echo -e "\033[0;33m")
ccend=$(echo -e "\033[0m")
/usr/bin/make "$@" 2>&1 | sed -E -e "/[Ee]rror[: ]/ s%$pathpat%$ccred&$ccend%g" -e "/[Ww]arning[: ]/ s%$pathpat%$ccyellow&$ccend%g"
return ${PIPESTATUS[0]}
}
Run Code Online (Sandbox Code Playgroud)
(最初通过Make中的突出显示警告.)
您可以使用tput命令和terminfo(5)数据库来移植.例如,
tput setf 5
Run Code Online (Sandbox Code Playgroud)
将终端作为标准输出,将前景色设置为紫色(或类似的东西;我是色盲). tput setf 0将前景色重置为默认值.
有关更多信息,请查找terminfo.