Search code examples
linuxshellcolorsxtermterminfo

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.


Solution

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