How Does Linux Terminal Work

Linux terminal is one of the most important development tools for software engineers (or production engineers). If you’d like to learn Linux, the first thing you might want to do is to get yourself familiar with terminal.

In this article, we’ll be briefly introducing Linux terminal.

What is Terminal?

When computers were invented, they were giant, which typically fit in a whole room. Like this

In order to interact with such a giant computer, people invented terminal, which is a much smaller device.

In short words, terminal is basically an input and output device for a computer.

After years of development, computers have become much smaller today. Today, when people refers to terminal, they do not mean the old terminal hardware device. What they mean is actually terminal emulator.

A terminal emulator is basically a software component which can emulate what the hardward devices had. For example,

Note that terminal is different from shells. Terminal is the GUI component that help you interact with the shells. Terminal does not understand what your input means. It simply forwards your input to the shells. Shells do not interact with you directly, but they do interpret whatever command stream that the terminal passes to it.

How Does Terminal Work?

The following diagram explains how terminal works (in a simple case, where a shell running behind the terminal).

Here’s the what the data flow looks like:

  1. Users type in characters in the terminal GUI.
  2. The characters are sent to the terminal UI component, which are then forwarded to the input component.
  3. The terminal input component then forwards to the characters to a pty master device.
  4. OS will forward the characters from the master pty device to the slave pty device.
  5. The characters are then forwarded from the slave device to the shell.
  6. The shell interprets the command stream, and then send out the output stream to the pty slave device.
  7. The output streams are forwarded back from the pty slave device to the pty master device, to the terminal output component, and finally to the terminal GUI.

What Data Flows Between Terminal And Terminal Applications?

The data flows between terminal and terminal applications are basically of two types:

  • Ascii Codes: This includes characters, enter, backspace, etc. Most of the data between terminal and terminal applications are in the form of ascii codes.
  • Escape Sequences: Controlling the bahavior of the terminal, for example, the text color, the cursor movement.

Terminal Examples

Today, there are many terminal implementations. Here are some examples:

  • Mac: iterm2, terminal
  • Windows: Mintty
  • Linux: xterm, urxvt, gnome terminal

Learn More

If you’d like to understand more about terminal, check out this udemy course. The course discusses how terminal works, and also helps you set it up to make your more productive.