测试Linux shell脚本中的颜色支持

los*_*imm 9 shell terminal colors termcap

这是我第二次想要这样做,而且我的google-fu再次失败了.

在运行shell脚本(在我的情况下是一个bash脚本)的过程中是否有一个程序/脚本来测试当前shell是否支持颜色?

或者有没有办法采取终端类型,并轻松确定它是否支持颜色?

无论哪种方式,它都会有所帮助.

Pau*_*ce. 16

你可以用tput colors.

对于我的终端TERM=xterm-256colors输出是[drumroll] 256!以下是其他一些例子:

$ TERM=vt100 tput colors
-1
$ TERM=vt220 tput colors
-1
$ TERM=linux tput colors
8
$ TERM=cons25 tput colors
8
$ TERM=linux tput colors
8
$ TERM=rxvt-unicode tput colors
88
Run Code Online (Sandbox Code Playgroud)

或者,tput -Tvt100 colors您也可以指定您感兴趣的终端类型.