How and where is $TERM interpreted?
$TERM
is read and interpreted by the terminfo system. terminfo also refers to the database of terminal descriptions which you can find on most systems in /usr/share/terminfo
. $TERM
must match one of the entries in that database. There was also an older library called termcap which had fewer capabilities, but terminfo has replaced it. In modern systems, terminfo is part of the ncurses library.
Applications usually either fetch terminal capabilities directly using library functions like tigetstr()
or they use higher level curses interfaces to manage the layout of the screen. Either way, $TERM
and the terminfo database will be consulted.
The TERM
variable is interpreted by each application, via system libraries. Its value is a name which is looked up in a database. Depending on the Unix flavor and its age, the database can be either termcap (traditional, not used much nowadays) or terminfo (modern, inasmuch as it was meant as an improvement on termcap, and used by most systems nowadays).
Both the termcap and terminfo databases associate capability names with values. Capabilities are either descriptions of what the terminal can do (number of lines, ability to underline, etc.) or strings that can be exchanged with the terminal (escape sequences to format text, move the cursor around, etc., and in the other direction escape sequences sent by function keys). You can look at man 5 termcap
and man 5 terminfo
to see what capabilities are known on your system.
For example, when you press Ctrl+L to redraw the screen, it reads capabilities in the terminal database to find out what escape sequences it must use to move the cursor around. If TERM
is not set or incorrectly set, screen has no way to know how to move the cursor around.
You can use the tput
command to retrieve entries in the terminfo database. For example tput lines
prints out the number of lines on the terminal. tput clear
clears the screen (because its output is getting printed on the terminal); to see what the corresponding escape sequence is, print it out in a readable form, e.g. tput clear | cat -v
.
The number of colors has quite a bit of history that makes it not work as it should by rights: terminal emulators tend to underreport their number of colors, to avoid breaking some applications. See tmux, TERM and 256 colours support for more on this, especially in the context of tmux. The difference between xterm-256colors
and xterm
is that the former reports 256 colors in terminfo while the latter reports the traditional 8.