Bash – Does “color” in TERM always mean I can use colors

bashbashrccolorsterminalterminal-emulator

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

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

Best Answer

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.