shell and terminal What is the terminal?

Terminal

Early computers were very expensive, a computer was used by many individuals, and everyone had a set of input and output devices. At that time, the input and output device was the terminal, and the physical hardware was the "teleprinter". Keyboard input, print jam output. Later, when monitors appeared, paper jams were no longer used, which was not environmentally friendly at all.
image.png

console console

The console is some physical control buttons embedded on the console, such as power on and off. Obviously, this is only accessible and usable by administrators.
Later, in order to manage the host in more detail, the administrator also needs a set of input and output devices, that is, a terminal, a terminal with higher authority. So the console is a more powerful terminal, and there is no difference in essence.
However, with the popularity of personal computers, the concept of console (Console) and terminal (Terminal) has gradually blurred. In modern times, our keyboards and monitors can be considered both consoles and ordinary terminals. They are consoles when you are administering the system, and terminals when you are doing general work (browsing the web, editing documents, etc.). We ourselves are both regular users and system administrators.
left console, right terminal

Terminal Emulator

With the evolution of computers, it is not a dream to have one computer for each person. We no longer see specialized terminal hardware, but keyboards and monitors instead.
In order to continue to use those traditional command-line programs that are not compatible with graphical interfaces, a terminal emulator has appeared to simulate the behavior of a physical terminal.

From the perspective of those traditional command-line (CLI) programs, a terminal emulator is a traditional terminal device; from the perspective of the graphical interface of a modern graphical operating system, a terminal emulator is a GUI program.

The standard workflow for a terminal emulator is this:

  1. Capture your keyboard input;
  2. Send input to the command-line program (the program will think it is input from a real terminal device);
  3. Get the output of the command line program (STDOUT and STDERR);
  4. Call the graphics interface (such as X11) to render the output result to the display.

There are many terminal emulators, here are a few classic examples:

  • GNU/Linux:gnome-terminal、Konsole;
  • macOS:Terminal.app、iTerm2;
  • Windows: Win32 console, ConEmu, etc.

In the modern era when specialized terminal hardware has basically only existed in computer museums, people usually call the terminal emulator a "terminal" to save trouble.

Terminal Window and Virtual Console

With the popularity of graphical user interfaces, most terminal emulators run in a graphical user interface (GUI), but there are exceptions.

For example, in the GNU/Linux operating system, pressing Ctrl + Alt + F1, F2...F6 and other key combinations can switch to several dark full-screen terminal interfaces, and pressing Ctrl + Alt + F7 is to switch back to the graphical interface. But don't be fooled by them, although they don't run in a graphical interface, they are actually a kind of terminal emulator.

The only difference between these full-screen terminal interfaces and those running under the GUI terminal emulator is that they are provided directly by the operating system kernel . These terminal interfaces directly provided by the kernel are called virtual consoles (Virtual Console) , and the above-mentioned terminal emulators running on a graphical interface are called terminal windows (Terminal Window) . Other than that there is no difference.

Of course, because the terminal window is running on the graphical interface, if the graphical interface crashes, they will also be finished. At this time, you can at least switch to the Virtual Console to fight the fire, because they are provided directly by the kernel, as long as the system itself does not have any problems, it is generally available (laughs).

TTY

In simple terms, tty is the general term for terminal.

why? Students who read Section 2.1 above should know that the earliest Unix terminal was the ASR-33 teletypewriter. The English abbreviation of Teletype / Teletypewriter is tty, which is the source of the name tty.

Since Unix was designed as a multi-user operating system, people would have multiple terminals (in those days, all teletypes) connected to the computer. In order to support these teletypewriters, the Unix system designed a subsystem named tty (yes, because the terminals at that time were all tty, so this system was also named tty, it is so simple and rude), the specific hardware device Abstracted to device files located in /dev/tty* inside the operating system.

Why abstract the hardware device of the teletypewriter into a "tty device" file? Interested students can learn about the concept of Everything is a file in the Unix operating system.

image.png
Remember the special terminals we mentioned above, that is, those virtual consoles (Virtual Console) that are called out through Ctrl + Alt + F1-6? It corresponds to tty1 to tty6 in the figure above.

With the development of computers, terminal devices are no longer limited to teletypewriters, but the name tty still remains. Over time, their concepts have been confused. So in modern times, a tty device is a terminal device, and a terminal device is a tty device, there is no need to distinguish.

Since the serial port (Serial Port) on early computers was mostly used to connect terminal devices, Unix at that time would also abstract the devices on the serial port as tty devices (located in /dev/ttyS*). Therefore, people often refer to serial devices as tty devices now.

In the tty subsystem, concepts such as pty, ptmx, and pts were derived later, which will not be expanded here in detail. Interested students can refer to this article: https://segmentfault.com/a/1190000009082089

Shell

Everyone knows that the operating system has something called the kernel (Kernel), which manages the hardware of the entire computer and is the most basic part of a modern operating system. However, the kernel is at the bottom of the system and cannot be operated by ordinary users at will, otherwise the system will crash if one is not careful!

But we always have to let the user operate the system, what should we do? This requires a special program that accepts commands entered by the user, then helps us communicate with the kernel, and finally lets the kernel complete our tasks. The program that provides the user interface is called Shell (shell).

Shell can generally be divided into two types: **command line shell** and graphical shell .
As the names suggest, the former provides a command-line interface (CLI) and the latter provides a graphical user interface (GUI). explorer.exe under Windows is a typical graphical shell (yes, it is, because it accepts instructions from you, and will help you interact with the kernel to complete your instructions).
Broadly speaking, the entire desktop environment is a shell that accepts instructions to the kernel and returns results.

Common or historically known command line shells are:

  • For Unix and Unix-like systems:
    • sh (Bourne shell), the most classic Unix shell;
    • bash (Bourne-Again shell), currently the default shell for most Linux distributions;
    • zsh (Z shell), my personal favorite shell;
    • fish (Friendly interactive shell), a shell that focuses on ease of use and friendly user experience;
  • cmd.exe (Command Prompt) and PowerShell under Windows.

The relationship between shell and terminal

The terminal contains the shell, because the terminal is just an input and output device, and the shell actually interacts with the system kernel.

The job of the terminal is to receive input from the user (keyboard, mouse and other input devices), throw it to the Shell, and then display the results returned by the Shell to the user (such as through a monitor). The job of the Shell is to get the command input by the user from the terminal, parse it and hand it over to the operating system kernel for execution, and return the execution result to the terminal.

In the desktop graphical operating system, open a terminal, the real name is a terminal emulator or a terminal window, and a shell is running in it.
For example, the terminal in windows is conhost.exe, opening it will automatically start cmd.exe. Because the terminal cannot function without the shell, cmd.exe is the shell in windows.
Similarly, opening the shell of the command line will also start a terminal, otherwise it will not be possible to input and output. There is another shell in windows is PowerShell, opening it will also start the conhost.exe terminal by default.


https://segmentfault.com/a/1190000016129862

Guess you like

Origin blog.csdn.net/qq_43220213/article/details/130457666