Linux Programming – Part 7: Shell and Terminal

Let’s dive into the world of terminal and shell, shall we?

Here, we’ll explore Linux’s two fundamental user interfaces: shell and terminal.

Terminal

in our discussion, let’s define terminal as the physical user interface, we, humans, can interact with, specifically our PCs. From our understanding of PCs, the concept of terminal is something we might struggle to fully grasp. The very original UNIX computers are quite different from what we have today.

As I described in a previous post, people used to share one single computer machine among multiple people. Specifically, the host computer wasn’t connected to either a monitor or a keyboard. Instead, multiple other hardware was connected to it.

To better understand what it actually means, let’s get back in time, and see how people back then interacted with computers.

History of terminal

Teletype was the hardware that was commonly used in the initial days of UNIX. Since they didn’t have monitors, the machine used to print out every calculated result into the actual pieces of paper. And for the human interface, they used to use type-writers. In the UNIX world, they call terminal tty which comes from TeleTYpe.

Dumb terminal took over the teletype, and this device had a monitor, keyboard, and other peripheral hardware. Because the device didn’t have any ability to calculate like a computer, it was called dumb. The most well-known dumb device was DEC’s VT100.

The early dumb devices like VT100 were called character terminal and was only able to display numbers and characters on the screen. Based on our common sense, it’s hard to imagine a device that can only display characters. To better understand what it was like, think about a cheap calculator. It has 8-shaped lines and is capable of displaying only numbers. The character terminal was just like that and of course, was colorless.

On the other hand, our modern displays create all sorts of images with a tremendous number of small dots. We call them bitmap display. Even in the early days, devices with bitmap display started to appear as years went by, and X terminal, which powered X Window System, was one of them.

And the latest terminal we have today is literally terminal emulator. This is what transformed the old hardware terminal into a software program, and good examples are GNOME terminal, Knosole, macOS’s Terminal.app, and iTerm2. This is the program that allows users to interact with the traditional terminal within the modern and sleek bitmap display.

So, terminal is an umbrella term that includes teletype, bitmap display terminal, and software terminal. So, what’s common among them is the concept that allows users to interact with their computers’ shell.

Virtual Console

Okay, here’s something you don’t want to hear. It’s a bit complicated, but don’t worry – you’re not alone haha… Since we mentioned terminal, we need to talk about the virtual console. When you interact with the terminal, you’re actually not interacting with the software directly – there’s something in between, which is the virtual console. When you have finished installing Linux and use it in the default settings, there are multiple numbers of virtual consoles running on the system. You may have seen some white lines of codes running on the black screen when booting your Linux PC, and this is one of those virtual consoles. You can switch them by clicking Ctrl + Alt + Fn. However, you’re not allowed to switch them when X Windows System is running.

Character terminal and ASCII

Character terminal was only able to handle characters when it comes to input/output. In this logic, terminal should only be able to handle character info.

On the other hand, in our modern PCs, the computer already prepared an image of the character a, which is going to be displayed on the screen, on the memory, and is waiting for the command.

So, how do they do that? In our modern computers, every character corresponds with a number, and they only receive the number to display its corresponding character to the display. ASCII (American Standard Code for Information Interchange) is the most commonly used protocol and is used as the default character protocol in our modern machines, including Linux.

Here is the table of the protocol:

image 01: ASCII table

The vertical line represents tens digit while the horizontal ones digit. For example, A is 65, B is 66, and a is 97.

So, what about other weird signs that appear in the earlier numbers, such as NUL or DEL? Well, those are numbers corresponding to other commands than just displaying characters. LF, corresponding to 10, is \n in C language, which is starting a new line. An obvious one is BEL, corresponding to 7, and it literally rings a bell when sending this code to a character terminal. Even in our modern PCs, the below code could ring a bell.

bell.c

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[]) {
    printf("\007");
    exit(0);
}

ASCII used to be designed for sending commands to a character terminal, but nowadays, its initial purpose is lost, being used as a character corresponding protocol as well as other functions.

image 02: ASCII – other functions

Terminal as a file, terminal as streaming

In our previous discussion, we talked about Linux’s three essential concepts: files system, process, and streaming. Here, let’s connect the dots by talking about how terminal has something to do with this discussion.

Given the fact that SSD and HDD are handled as device files, UNIX is good at handling everything as files. Terminal is not an exception, and every corresponding device is located in /dev directory. The first virtual console corresponds to /dev/tty0, and the second /dev/tty1.

So, what’s the point of handing them as files? It essentially comes down to getting the stream (connection), so that the system can receive commands from keyboards and display the output result to the screen.

image 03: terminal and streaming

Hence, because of this mechanism, terminal is abstracted as files and streaming processes.

Shell

Finally, let’s discuss what shell is. Shell is an interpreter between a user and the kernel, translating user-input commands into an actual program to execute them. sh, bash, csh, tcsh, zsh, ksh, and ash are the major shells in our modern Linux systems. And shell is just a program that is booted when a user is logging in as well as receiving commands, and executing them.

Leave a Reply