Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for color support in Linux shell scripts

This is the second time I've wanted to do this and again my google-fu has failed me.

When in the course of running a shell script (in my case a bash script) is there a program/script that tests whether the current shell supports color?

Alternatively is there a way to take the terminal type and easily determine if it supports color?

Either way it would be helpful.

like image 633
lose_the_grimm Avatar asked Sep 15 '10 23:09

lose_the_grimm


1 Answers

You can use tput colors.

For my terminal with TERM=xterm-256colors the output is [drumroll] 256! Here are some other examples:

$ 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

Alternatively tput -Tvt100 colors will also allow you to specify the terminal type you're interested in.

like image 180
Dennis Williamson Avatar answered Oct 21 '22 17:10

Dennis Williamson