6

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?

Shuzheng
  • 4,411
  • These questions are way too broad, and an answer will turn into a treatise, but for the last of them it's simple: GNU ls and grep do NOT care about the TERM variable, they simply assume that any terminal supports ANSI color escapes. –  Jul 04 '19 at 07:24
  • 1
    You can use this occasion to submit a doc patch to gettext to fix the spelling of "cababilities" in that manual. –  Jul 04 '19 at 07:53

2 Answers2

13

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.

Stephen Kitt
  • 434,908
  • Thanks for your answer. Do you know if there is any canonical documentation available for TERM variable? I can't really seem to find any. Since it is mentioned so often, I guess the variable is pretty "standardized"? for a start, the terminal doesn’t know what TERM is set to! - the terminal doesn't have access to an environment variable set in the shell, right? In ...how to determine a terminal’s capabilities, I guess you say the same thing twice? Does TERM determine if a program ouputs ANSI color escapes? Also, why doesn't the shell cares about TERM? It outputs text too (e.g. prompt)? – Shuzheng Jul 04 '19 at 08:13
  • 1
    TERM is described in POSIX, but its values are unspecified; ncurses is possibly the best source of information nowadays. Yes, the terminal doesn’t have access to the shell’s environment variables. Determining capabilities and using features aren’t quite the same thing, although a decent terminal definition won’t say how to access features it doesn’t support ;-). Properly-written programs don’t know anything about ANSI escapes; they ask the terminal database how to do the things they want to do. – Stephen Kitt Jul 04 '19 at 08:52
  • 1
    @StephenKitt most advanced shells (bash, zsh, ksh) will use terminal escapes to implement their line editing caps. They will consider the TERM envvar when doing it (zsh will even use xterm's mouse escapes to implement text selection itself -- better said to break the text selection by stuffing it with whitespace). –  Jul 04 '19 at 09:18
  • @UncleBilly I stand corrected, I’ve updated the answer. – Stephen Kitt Jul 04 '19 at 09:22
  • @Shuzheng the TERM envvar is documented in the ncurses(3) manpage -- which tells you everything you need to know about it. Better just read it. –  Jul 04 '19 at 09:22
  • 2
    terminal emulators (and getty) are typically the ones that set the TERM env var. – Stéphane Chazelas Jul 04 '19 at 09:26
  • Thanks again. These map the value of TERM to a database of capabilities... and ..and how to determine a terminal’s capabilities. Aren't these things the same? Obtaining a database of the terminal's capabilities amounts to determining the terminal's capabilities? – Shuzheng Jul 04 '19 at 10:13
  • You’re right, I’ve rephrased that part. – Stephen Kitt Jul 04 '19 at 10:32
2

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.

iBug
  • 3,508
  • Thank you for the htop mention, it provides a straightforward way to test TERM settings e.g. xterm-256color or xterm-mono. – TFuto Sep 15 '23 at 21:31