In lots of places, depending
On virtual terminals and real terminals, the
TERM environment variable is set by the program that chains to
login, and is inherited all of the way along to the interactive shell that executes once one has logged on. Where, precisely, this happens varies from system to system, and according to the kind of terminal.
Real, serial, terminals can vary in type, according to what's at the other end of the wire. So conventionally the
getty program is invoked with an argument that specifies the terminal type, or is passed the
TERM program from a service manager's service configuration data.
Kernel virtual terminals, as you have noted, have a fixed type. Unlike NetBSD, which can vary the kernel virtual terminal type on the fly, Linux and the other BSDs have a single fixed terminal type implemented in the kernel's built-in terminal emulation program. On Linux, that type matches
linux from the terminfo database. (FreeBSD's kernel terminal emulation is a limited
xterm subset since version 9.)
- On systems using
vc-get-tty (from the nosh package) the program "knows" that it can only be talking to a virtual terminal, and they hardwire the "known" virtual terminal types appropriate to the operating system that the program was compiled for.
- On systemd systems, one can see this in the
/usr/lib/systemd/system/getty@.service unit file (
/lib/systemd/system/getty@.service on un-merged systems), which reads
Environment=TERM=linux setting the
TERM variable in the environment passed to
For kernel virtual terminals, one does not change the terminal type. The terminal emulator program in the kernel doesn't change, after all. It is incorrect to change the type. In particular, this will screw up cursor/editing key CSI sequence recognition. The
linux CSI sequences sent by the Linux kernel terminal emulator are different to the
vt100 CSI sequences sent by GUI terminal emulator programs in DEC VT mode.
Your GUI terminal emulator is one of many programs, from the SSH dæmon to
screen, that uses pseudo-terminals. What the terminal type is depends from what terminal emulator program is running on the master side of the pseudo-terminal, and how it is configured. Most GUI terminal emulators will start the program on the slave side with a
TERM variable whose value matches their terminal emulation on the master side. Programs like the SSH server will attempt to "pass through" the terminal type that is on the client end of the connection. Usually there is some menu or configuration option to choose amongst terminal emulations.
The gripping hand
The right way to detect colour capability is not to hardwire a list of terminal types in your script. There are an awful lot of terminal types that support colour.
The right way is to look at what termcap/terminfo says about your terminal type.
if tput Co > /dev/null 2>&1
test "`tput Co`" -gt 2 && colour=1
elif tput colors > /dev/null 2>&1
test "`tput colors`" -gt 2 && colour=1
- Jonathan de Boyne Pollard (2018).
TERM. nosh Guide. Softwares.