4

Should the following always indicate that I can use escape sequences and tput to colorize things?

if [[ $TERM == *"color"* ]]; then
  true
else
  false
fi
4

No: the name of the terminal description is irrelevant. TERM is the name of a terminal description. There are a lot, and most of the terminals which support color do not have "color" in their names. There is for instance no reason why a terminal description could not be named "no-color".

Programs that use the terminal description look at the capabilities listed in it. There are of course a lot of hard-coded programs which assume things about the name of a terminal description.

According to the ncurses FAQ My terminal doesn't recognize color, the terminal description should say how many colors the terminal supports, as well as how to set the foreground and background colors.

You can use tput to obtain the number of colors:

colors=$(tput colors)
if [ $colors -gt 1 ]

since a missing colors capability makes tput return -1.

  • The tput colors in my terminal say 8 but the terminal does support 256 colors. – user79743 Mar 19 '16 at 18:11
  • 1
    Then the choice of TERM is incorrect (for your purposes). You could simply hardcode an assumption... – Thomas Dickey Mar 19 '16 at 18:20
  • There is nothing incorrect in using an stock terminal app. Maybe it is you who should review your assumptions ... – user79743 Mar 19 '16 at 18:50
  • To land in the middle. As question is "always indicate" I guess one could say that it is a in the middle per user configuration. If a user have chosen to use a no-color terminfo / termcap then that should likely be honored, even though the terminal might support colours. As well as if one use xterm even though it is xterm-256colors capable one are to use 8 and not 256 ... – Runium Mar 19 '16 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.