DEV Community

Cover image for Demystifying the Terminal: How It Works Behind the Scenes
Jessica Wang
Jessica Wang

Posted on • Updated on

Demystifying the Terminal: How It Works Behind the Scenes

If you've ever opened up a terminal and entered a command, you've witnessed the magic of computing in action. But have you ever wondered what happens under the hood to fulfill that seemingly simple command? The truth is, the inner workings of a terminal are surprisingly complex. In this article, we'll scratch the surface of how a terminal operates from a technical standpoint.


^ Youtube video if you prefer that. The visuals are a lot better here :-)

The Terminal's Historical Roots

Before we delve into the intricacies of modern terminals, let's take a trip back in time, over 60 years ago. The modern terminal we use today draws inspiration from teletype computers, which were designed to facilitate text-based communication through keyboards and printed output.

Image description

In the 1960s, many places with access to computing power relied on mainframe computers connected to teletype interfaces. Here's a simplified breakdown of how this technology functioned:

  1. Every keypress transmitted a character over a wire.
  2. The mainframe computer's kernel received and decoded this input.
  3. The decoded text was sent to a component called the TTY driver, responsible for relaying user input to programs and collecting output.
  4. Finally, the output was sent back to the user for display.

What made this system remarkable was the introduction of "line discipline," which buffered characters in memory until the Enter key was pressed. Over time, various optimizations, such as error correction and shortcuts, made modern terminals resemble their teletype predecessors.

Evolution of Terminals

In the 1970s, technology took a giant leap with the introduction of video terminals like the VT05 and VT100. These terminals supported ANSI escape codes, colors, sounds, and marked a revolution in computing. However, the core principle remained the same—input and output.

Image description

As technology progressed, computers ceased being mere tools to communicate with larger machines; they became ubiquitous in operating systems.

The Modern Terminal: An Application

Fast forward to today, and the terminals we use have evolved significantly. Computers no longer rely solely on text-based interactions. Instead, they handle a multitude of intricate processes simultaneously.

Image description

Modern terminals are now software applications, overseen by your chosen operating system. When you open a terminal, it listens for user input and instructs the operating system on what to display in the terminal window. Essentially, it emulates the hardware of the terminal.

Terminal Emulation

When you click on the terminal icon, behind the scenes, the terminal establishes a communication channel with your shell—the software responsible for executing commands. In the 1960s, this connection was a physical wire. Today, it's known as a pseudo-teletype or PTY, consisting of pairs of file descriptors that bridge communication between the terminal and running processes.

Image description

One end of the PTY is the "leader," used by the terminal to process user input and display output. The other end is the "follower," utilized by the shell and other tasks initiated by the terminal.

Spawning the Shell

After creating the necessary files, the terminal's critical task is spawning the shell—the command interpreter and the brains of the operation. Each time you enter a command, the shell runs it as a child process, with the terminal acting as the parent.

The terminal configures the child process to read and write from the PTY follower, establishing a seamless connection between user input and the shell.

Shell Initialization

Once the shell is born, it executes scripts to allow users to customize their experience. Depending on whether you're using a GUI-less server or a personal computer, the behavior may differ. Typically, the shell prints information like the username, time, hostname, and more, preparing itself to accept user input.

Running a Command

When you type a command, the characters are translated into Unicode characters, which are then processed by the shell's line editor. Special characters trigger specific functions, while non-special characters are passed to the line discipline.

Image description

Modern shells often disable the line discipline's canonical features, operating in raw mode. This allows the shell to handle all characters before submitting the command, enabling features like auto-completion, suggestions, and syntax highlighting.

Hitting Enter

Pressing Enter is when the real action happens. The shell parses the command by tokenizing and analyzing it. It checks various sources for validity, including aliases, functions, environment variables, built-in commands, and executable files in the PATH.

Once it identifies a valid source, such as an executable file, it locates it on the filesystem and runs it as a separate child process, passing any additional arguments.

Returning Output

When you run a command, like "ls," it spawns a child process that inherits the shell's file descriptors. All output from the command is directed to the PTY follower. The terminal emulator reads and displays the raw bytes streamed from the child process.

For the colorful text you see in modern terminals, escape sequences play a crucial role. These special codes embedded in text output instruct the terminal on how to render text, including colors, cursor movements, and more.

Closing the Terminal

When you close your terminal window, several steps occur behind the scenes:

  • The shell performs cleanup, such as saving command history and running logout scripts.
  • Child processes spawned by the shell are terminated.
  • Resources like open file descriptors are released.
  • The shell process exits and signals the terminal emulator to close.
  • The operating system cleans up any remaining resources.
  • The terminal window is fully closed, and your session is logged out and cleaned up.

All of this happens in a matter of seconds, showcasing the complexity hidden behind what seems like a simple command line.

Conclusion

The terminal, despite its apparent simplicity, remains the most straightforward way to communicate with a computer. Its evolution from teletypes to modern terminal emulators reflects the enduring power and adaptability of this technology.

Image description

If you want to try out a modern terminal that will boost your productivity, check out Warp.

Top comments (2)

Collapse
 
therubberduckiee profile image
Jessica Wang

I know comments on my previous blogs were asking for more "build cool stuff tutorials", but I wanted to publish this blog first. It's a bit of a history lesson, but cool stuff. Highly recommend watching the Youtube video for the visuals & editing (if you don't mind seeing my face pop up once in a while).

And don't worry, I've got a tutorial on how to build a ChatGPT clone on the terminal coming soon.

Oh and as always... gotta seed a fun question to keep the comment section lively:
If you had to eliminate pasta, rice, or noodles from your diet, which would it be?

Collapse
 
fyodorio profile image
Fyodor

don't worry, I've got a tutorial on how to build a ChatGPT clone

The most worrying part of all this story 😅

And pasta.