How can a shell script tell if it is running in an xterm window with 256-color support?

I'm writing a shell script that I would like to use 256-color support when present. In a just world, xterm would simply set the TERM environment variable to xterm-256color and I'd use tput colors to discover the support.

But we live in an unjust world. xterm sets TERM to xterm even when launched with TERM=xterm-256color. Is there anything I can do from within a shell script to discover whether the script is running with stdout connected to a tty running in a 256-color xterm window? If so, I can set the environment variable myself.


You can check for xterm version - $XTERM_VERSION. Though I am not sure from which version xterm started to support 256 colors. Other terminals (for example gnome-terminal), behave similarly to xterm but also set COLORTERM variable to correct terminal name (gnome-256color).

Since most xterm-s are colorized these days, I would assume that the terminal has 256 colors, and make the script accept an option (perhaps -monochrome or -no-colors) otherwise.

BTW, I also see a reason for some power-user to force the -monochrome behavior: on very slow connections (think of intercontinental ssh) it might be useful to disable colors to lower the bandwidth.

Need Your Help

Upload many files with symfony2

php symfony doctrine

I'd like to know how to upload many files at once with Symfony2 and docrine2 . At present, I can upload just one file

Play Framework 1.2.4: Close or Disconnect the session after bulk insert job?

hibernate jdbc playframework bulkinsert

i am working on a play 1.2.4 project and i have 4 jobs running in intervals, fetching bulk data (from webservice) and saves them to the database.