What uses the TERM variable?

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.


I'd say a lot of nCurses programs (and libtinfo ones) care about TERM.

One example would be htop, which often gives "partially-overwritten" lines when TERM=xterm, but everything looks OK when TERM=xterm-color.

From my experiences, programs that don't use libtinfo (either directly or indirectly, say via nCurses) usually don't care about TERM at all and assume support for color or cursor positioning, etc.

Tags:

Terminal

Xterm