How does the shell script indicate if it works in an xterm window with support for 256 colors?

I am writing a shell script that I would like to use with support for 256-color support. In a simple world, xterm simply set the TERM environment variable to xterm-256color , and I would use tput colors to detect support.

But we live in an unfair world. xterm sets TERM to xterm even at startup using TERM=xterm-256color . Is there anything I can do from a shell script to find out if the script works with stdout connected to tty running in a 256-color xterm window? If so, I can set the environment variable myself.

+6
source share
2 answers

You can check the xterm version - $ XTERM_VERSION. Although I'm not sure which version xterm started to support 256 colors from. Other terminals (e.g. gnome-terminal) behave similarly to xterm, but also set the COLORTERM variable to correct the terminal name (gnome-256color).

+2
source

Since most xterm -s are painted these days, I would assume that the terminal has 256 colors, otherwise the script will accept a parameter (possibly -monochrome or -no-colors ).

By the way, I also see the reason why some force user can force -monochrome behavior: with very slow connections (think about intercontinental ssh ), it would be useful to disable colors to reduce bandwidth.

0
source

Source: https://habr.com/ru/post/918352/


All Articles