Originally, "tty" had two definitions: the hardware (now the emulator) and the driver (interfaced through /dev/pty* or /dev/tty*).
The hardware/emulator was/is responsible for:
- Taking a stream of data and presenting it; this included interpreting control sequences like "move cursor left", "blinking cursor", "clear-screen" although these control sequences were often different among manufacturers.
- Sending keycodes for keys typed by the user; most of these were standard ASCII characters, but some terminals sent propriety keycodes for even standard keys.
The 'tty' driver was responsible for:
- Managing the buffering, in raw or canonical mode; for example, buffering a line of characters until Enter is pressed.
- Managing the control flow; e.g. being able to stop/continue with Cntl-s/Cntl-q.
- Translating propriety keycodes to standard ASCII, where applicable.
- Intercepting certain control characters (like Cntl-c and Backspace) and processing them appropriately (send SIGINT on a Cntl-c or signal an EOF on Cntl-d.
- Canonical display of characters, for example, if
echo
is turned off, then do not send feedback (character typed) back to the terminal.
The terminfo and termcap databases managed what terminal control characters should be sent for an operation (like 'clear-screen'). These control sequences were/are not interpreted by the driver, but by the hardware/emulator.
For the most part, you should not set TERM
manually. The variable is always set automatically, and there is only a narrow set of circumstances where the default value is incorrect.
The value of TERM
needs to be referenced in the system's terminfo database (or for a few old-fashioned system, in the termcap database). Terminfo and Termcap map terminal types to the description of terminal capabilities that applications use. The value of TERM
is the terminal type.
The main reason why you might sometimes need to change TERM
is if you log in remotely, when the local machine and the remote machine have different terminal databases.
Another reason, which is related to the previous one, is that sometimes there are multiple similar entries for a terminal, with slightly different capabilities. This tends to happen mostly when a new terminal comes up which is compatible with an existing terminal, but has more features. You then get a choice between using the traditional name, which all machines understand, but which advertises only the traditional features, or the newer name, which advertises all the new features but which some machines won't understand.
An example of this is xterm with 16 colors vs xterm with 256 colors. A traditional xterm supports only 16 colors, so that's what the xterm
terminal database specifies. Changing the xterm
entry would make users of the newer xterm versions happy, but would break the configuration of users of older xterm versions who log in remotely. Switching to a new name — xterm-256color
— provides the newer capabilities whenever available, but is not recognized if you log in remotely to machines with an older terminal database. Because there is no solution that can satisfy everyone, you get a choice: either stick with xterm
and don't get the 256 colors, or use xterm-256color
which doesn't work if you log in to remote machines that don't support it.
Leaving the default value of xterm
and setting the termName
resource to xterm-256color
(which causes the environment variable TERM
to be set to the same value) are thus both sensible choices. Setting TERM
to arbitrary values wouldn't work, but both xterm
and xterm-256color
make sense.
Best Answer
The
TERM
variable is used by programs running in a terminal. It is supposed to allow programs to determine the capabilities of the terminal (or emulator) which is handling their output. It is documented in thencurses
manpage.The terminal itself, including emulators such as
xterm
, doesn’t care about the value ofTERM
, beyond setting it (in the case of emulators — physical terminals can’t). It knows how to handle certain output sequences, and it handles them, without caring aboutTERM
or anything else apart from its internal state. You can setTERM
to anything you like in your shell, or even unset it, without changing the terminal’s behaviour; for a start, the terminal doesn’t know whatTERM
is set to!Programs which care about
TERM
are typically those which use an output library which cares, such as ncurses, or in a more basic form Termcap or Terminfo. This includes shells such as Bash and Zsh, which use terminfo, for example for line editing features (being able to erase the line when you move up and down the history). These map the value ofTERM
to a database of capabilities, which tell the program or library whether the terminal can perform certain tasks (such as moving the cursor, clearing the screen, changing colours) and how to go about it. Some programs, such as GNUgrep
, assume capabilities without even checking.Changing
TERM
fromxterm-256color
toxterm
won’t change much, in particular it won’t disable colour support in programs which refer toTERM
:xterm
supports colour output too. The difference is in the number of colours which are supported.See How do keyboard input and text output work?, Colors in Man Pages, Which terminal type am I using?, What protocol/standard is used by terminals? for more detail.