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

时间:2010-09-15 23:08:21

标签: shell colors terminal termcap

这是我第二次想要这样做,而且我的google-fu再一次让我失望。

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

或者有一种方法可以采用终端类型并轻松确定它是否支持颜色?

无论哪种方式都会有所帮助。

1 个答案:

答案 0 :(得分: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

或者tput -Tvt100 colors也允许您指定您感兴趣的终端类型。