Demystifying the Terminal: How It Works Behind the Scenes
Published: 2023-09-06 21:03:41

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.

{% embed https://www.youtube.com/watch?v=2P7fcVHxA9o %}
^ 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7t9fbo2kr603z1tupg7.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pxbn78u4enp51qhhmdlz.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0axkelqh7d3c9thty8t1.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8199gmkfyg0ccw5sq3fv.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ityok77p2wus0ajdkttn.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dexxfng7yyjteagvckr7.png)

If you want to try out a modern terminal that will boost your productivity, check out [Warp](warp.dev).