TERM
, by convention refers to a terminal description. Originally this named a section of a termcap text-file (starting in the late 1970s). In the mid-1980s, terminfo was introduced as a compiled (binary) file which saved time when obtaining the terminal description. While both are available for all Unix-like platforms, termcap is rarely used today except as an emulation using terminfo.
For both of these data formats, applications usually extract data from the terminal database with reusable programming libraries. The terminfo programming library is usually part of the higher-level curses library although it may be provided (e.g., optionally as in ncurses) as a separate library file. Whether the terminfo library is provided separately or not, in these cases it is considered part of the curses library. (There are also a few other higher-level libraries such as slang).
The terminal database entry for each terminal contains properties referred to as capabilities. They tell the curses library (or applications using termcap/terminfo directly) how to do useful operations such as clear the screen. For most terminals that is an escape sequence. A few terminals may not support an escape sequence for this purpose; there are other capabilities which may be combined by the curses library to clear the screen (such as clearing each line). Not all capabilities are escape sequences. There are boolean and number capabilities as well, e.g., to tell whether a feature is supported, and how large something is (such as screen size).
Each application which uses termcap/terminfo uses the corresponding library to retrieve the terminal description, as well as to perform operations such as substituting parameters into certain capabilities. For example, most terminals provide a capability to move the cursor by a given number of columns or rows from its current location. The tparm
(or tiparm
) functions substitute the number into the capability to obtain the actual escape sequence.
The curses library has command-line applications which maintain the terminal database (tic, infocmp) and some which are used in shell scripts for querying the terminal database or performing low-level operations with terminal capabilities (tput, tset/reset).
There are unconventional applications which use TERM
without using the terminal database. Most of these simply hardcode their behavior (such as GNU grep, groff, and the links/links2/elinks textual web browsers), while a few have what amounts to their own terminal database (such as GNU ls), but using different rules and behavior.
Back to the question(s):
So where is this variable interpreted and allows for example resetting my terminal screen using CTRL+l if I set the right value there?
The application and the underlying libraries interpret this value. For controlL, that may be done for bash in the readline library (which uses a termcap programming interface).
Who checks for example which colors are supported (xterm vs xterm-256color)?
The terminal database stores the number of colors as a capability, along with capabilities for setting the foreground and background colors and resetting colors. Some applications combine these capabilities with other information (such as a developer's assertion that xterm
is "really" xterm-256color
).
The shell?
Most shells use a termcap programming interface to obtain the terminal information. However, they are applications which have their own behavior (not necessarily the same as curses).
The application or a library like ncurses?
(see above: shells are a particular type of application)
And where are the possible values / terminal types defined?
Usually that is in a terminal database shared by applications using the curses or slang libraries. Some applications are hardcoded or use a private database.
Further reading: