Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 857
Norman Ramsey Avatar asked Jun 18 '12 12:06

Norman Ramsey


1 Answers

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).

like image 190
Leonid Volnitsky Avatar answered Sep 17 '22 10:09

Leonid Volnitsky