Introduction To Terminal

This article gives an introduction to terminal.

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, I’ll be briefly introducing terminal.

What is Terminal?

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

computer

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

terminal

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,

linux remote development environment snapshot

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).

how terminal works

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 Codes: Controlling the bahavior of the terminal, for example, the text color, the cursor movement.

The escape codes some are also called escape sequences. It’s important to make sure that your terminal is capable of generating these escape codes. However, most of terminals do not come with a complete set of escape codes by default, e.g., iterm2, gnome terminal, mintty, putty. That means, you will not able to use certain hotkeys in these terminals.

Fortunately, there are workarounds. Some of these terminial allow you to configure escape codes, some of the terminal can be enabled to send a relatively complete set of escape codes. I’ll talk about them in separate articles.

Terminal Examples

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

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