1. Overview

In this tutorial, we’ll learn about the $TERM environment variable. We’ll explore the meaning behind the values the variable holds. In addition, we’ll also cover the terminfo database that contains the capabilities of terminal types.

Finally, we’ll discuss the xterm terminal emulator and why modern terminal emulators advertise themselves as xterm.

2. The $TERM Environment Variable

The $TERM environment variable originated during the dawn of the computing era. During that time, terminals were the primary medium to interact with computers. It plays an important role in providing system compatibility and personalization.

Most beginners confuse the $TERM environment variable with the actual terminal name, which isn’t the case. The $TERM environment variable, usually defined in the shell profile, is a variable that indicates the terminal type.

A terminal type specifies the model of the terminal emulator. Some of the popular terminal types include xterm, xterm-256color, vt100gnome-terminal, and rxvt-unicode. Each of these terminal types provides different capabilities.

2.1. Purpose of $TERM

The $TERM environment variable is used by applications to take advantage of the capabilities of that terminal emulator. For instance, if a program wants to display colored text, it would first check whether the terminal supports colored text.

Not only does it provide the supported capabilities of the terminal, but also how to use those capabilities. Therefore, we can say that the $TERM variable is useful in cases when an application needs to adapt its behavior to different terminal types.

2.2. Terminal Type Database

The way the terminal-type database works is that the Linux system keeps a library that contains terminal types and their features. The location of the library is specific to the Linux distribution. However, it’s located in one of the following directories:

  • /etc/terminfo
  • /usr/share/terminfo
  • /lib/terminfo
  • /etc/termcap

On Ubuntu, the terminal-type database is located in /lib/terminfo. In addition, on legacy Linux machines, the terminal-type library is located in /etc/termcap.

The library contains directories that are named alphabetically, as illustrated below:

$ ls /lib/terminfo
a c d E h l m p r s t v w x

Each of these directories represents a specific range of terminal types. For example, the xterm capabilities file is located in the x directory. Inside the x directory, we’ll find the xterm file that contains the following text:

$ cat /lib/terminfo/x/xterm
=&ùxterm|xterm-debian|xterm terminal emulator (X Window System)
...

As we can see, it’s a binary file. This file contains the capabilities for the xterm terminal type. More specifically, it includes escape codes, control sequences, and other features that are specific to the xterm terminal emulator.

2.3. Default Value of $TERM

The default value of the $TERM variable is specific to the distribution in use. For example, on Ubuntu, the default value of $TERM is xterm-256color:

$ echo $TERM
xterm-256color

xterm-256color is a variant of xterm that supports 256 different colors. However, the older xterm configurations support eight or 16 different colors.

2.4. A Simple Example

As an example, we’ll use the xterm terminal type to print text in red color:

$ TERM=xterm tput setf 4; echo "Hello, World\!"
Hello, World!

Let’s break this down:

  • TERM=xterm sets the terminal type to be xterm, which informs our terminal that the preceding capabilities belong to the xterm terminal type
  • tput is a command we can use to interact with terminal capabilities
  • setf capability sets the foreground color, which in our case is 4

The color codes will vary depending on the terminal color palette and terminal type. On the vanilla xterm terminal using the xterm terminal type, it appears to be red. It’s also true for the majority of other terminal emulators:

xterm Colored Text

3. The xterm Terminal Type

xterm is a GUI terminal that belongs to the X Window System set of utilities. It was developed in the mid-1980s. It’s compatible with a wide range of utilities. Apart from that, most Linux machines most likely have a definition for the xterm terminal type in the terminfo library.

Due to its compatibility with most applications, new terminal emulators use the xterm terminal type as well because the system terminfo library doesn’t have a definition for it yet. Therefore, most programs running on the new terminal will fail because the terminal isn’t known.

For that reason, other terminals like alacrittykittyxfce4-terminal set $TERM to xterm to get around the compatibility issues. However, it comes with a cost because most modern terminals support more features than xterm. So, at the cost of fewer features, most modern terminals advertise themselves as xterm to opt for more compatibility.

Furthermore, terminal emulators that are not xterm may include their own entry in the terminal database using their specific name. However, it doesn’t mesh well with remote shells.

4. Conclusion

In this article, we discussed the $TERM environment variable, which defines the capabilities of a terminal. Then, we learned about the terminfo database that contains definitions for different terminal emulators.

Finally, we looked at the xterm terminal emulator and its primary use case.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.