terminal,xterm – Understanding the TERM Variable Usage

terminalxterm

According to this document:

The environment variable TERM contains a identifier for the text
window’s capabilities. You can get a detailed list of these
cababilities by using the ‘infocmp’ command, using ‘man 5 terminfo’ as
a reference.

But how is the TERM variable actually used? Say, the system runs xterm (terminal emulator for the X Window System). Does xterm use the TERM variable, or is it the shell? If so, how? Would xterm stop working, if TERM is set to linux?

Also, why isn't colored output disabled if I change TERM from its default value of xterm-256color to something else like xterm?

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 the ncurses manpage.

The terminal itself, including emulators such as xterm, doesn’t care about the value of TERM, 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 about TERM or anything else apart from its internal state. You can set TERM 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 what TERM 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 of TERM 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 GNU grep, assume capabilities without even checking.

Changing TERM from xterm-256color to xterm won’t change much, in particular it won’t disable colour support in programs which refer to TERM: 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.

Related Question