控制台,终端,tty,shell等概念的区别
(一)
A terminal is at the end of an electric wire, a shell is the home of a turtle, tty is a strange abbreviation and a console is a kind of cabinet.
Well, etymologically speaking, anyway.
In unix terminology, the short answer is that
- terminal = tty = text input/output environment
- console = physical terminal
- shell = command line interpreter
Console, terminal and tty are closely related. Originally, they meant a piece of equipment through which you could interact with a computer: in the early days of unix, that meant a teleprinter-style device resembling a typewriter, sometimes called a teletypewriter, or “tty” in shorthand. The name “terminal” came from the electronic point of view, and the name “console” from the furniture point of view. Very early in unix history, electronic keyboards and displays became the norm for terminals.
In unix terminology, a tty is a particular kind of device file which implements a number of additional commands (ioctls) beyond read and write. In its most common meaning, terminal is synonymous with tty. Some ttys are provided by the kernel on behalf of a hardware device, for example with the input coming from the keyboard and the output going to a text mode screen, or with the input and output transmitted over a serial line. Other ttys, sometimes called pseudo-ttys, are provided (through a thin kernel layer) by programs called terminal emulators, such as Xterm (running in the X Window System), Screen (which provides a layer of isolation between a program and another terminal), Ssh(which connects a terminal on one machine with programs on another machine), Expect (for scripting terminal interactions), etc.
The word terminal can also have a more traditional meaning of a device through which one interacts with a computer, typically with a keyboard and display. For example an X terminal is a kind of thin client, a special-purpose computer whose only purpose is to drive a keyboard, display, mouse and occasionally other human interaction peripherals, with the actual applications running on another, more powerful computer.
A console is generally a terminal in the physical sense that is by some definition the primary terminal directly connected to a machine. The console appears to the operating system as a (kernel-implemented) tty. On some systems, such as Linux and FreeBSD, the console appears as several ttys (special key combinations switch between these ttys); just to confuse matters, the name given to each particular tty can be “console”, ”virtual console”, ”virtual terminal”, and other variations.
See also Why is a Virtual Terminal “virtual”, and what/why/where is the “real” Terminal?.
A shell is the primary interface that users see when they log in, whose primary purpose is to start other programs. (I don't know whether the original metaphor is that the shell is the home environment for the user, or that the shell is what other programs are running in.)
In unix circles, shell has specialized to mean a command-line shell, centered around entering the name of the application one wants to start, followed by the names of files or other objects that the application should act on, and pressing the Enter key. Other types of environments don't use the word “shell”; for example, window systems involve “window managers” and “desktop environments”, not a “shell”.
There are many different unix shells. Popular shells for interactive use include Bash (the default on most Linux installations), zsh (which emphasizes power and customizability) and fish (which emphasizes simplicity).
Command-line shells include flow control constructs to combine commands. In addition to typing commands at an interactive prompt, users can write scripts. The most common shells have a common syntax based on the Bourne_shell. When discussing “shell programming”, the shell is almost always implied to be a Bourne-style shell. Some shells that are often used for scripting but lack advanced interactive features include the Korn shell (ksh) and many ash variants. Pretty much any Unix-like system has a Bourne-style shell installed as /bin/sh, usually ash, ksh or bash.
In unix system administration, a user's shell is the program that is invoked when they log in. Normal user accounts have a command-line shell, but users with restricted access may have a restricted shell or some other specific command (e.g. for file-transfer-only accounts).
The division of labor between the terminal and the shell is not completely obvious. Here are their main tasks.
- Input: the terminal converts keys into control sequences (e.g. Left →
\e[D). The shell converts control sequences into commands (e.g.\e[D→backward-char). - Line edition, input history and completion are provided by the shell.
- The terminal may provide its own line edition, history and completion instead, and only send a line to the shell when it's ready to be executed. The only common terminal that operates in this way is
M-x shellin Emacs.
- The terminal may provide its own line edition, history and completion instead, and only send a line to the shell when it's ready to be executed. The only common terminal that operates in this way is
- Output: the shell emits instructions such as “display
foo”, “switch the foreground color to green”, “move the cursor to the next line”, etc. The terminal acts on these instructions. - The prompt is purely a shell concept.
- The shell never sees the output of the commands it runs (unless redirected). Output history (scrollback) is purely a terminal concept.
- Inter-application copy-paste is provided by the terminal (usually with the mouse or key sequences such as Ctrl+Shift+V or Shift+Insert). The shell may have its own internal copy-paste mechanism as well (e.g. Meta+W and Ctrl+Y).
- Job control (launching programs in the background and managing them) is mostly performed by the shell. However, it's the terminal that handles key combinations like Ctrl+C to kill the foreground job and Ctrl+Z to suspend it.
(二)
A terminal or a console is a piece of hardware, using which a user can interact with a host. Basically a keyboard coupled with a text screen.
Nowadays nearly all terminals and consoles represent "virtual" ones.
The file that represents a terminal is, traditionally, called a tty file. If you look under the "/dev" directory of a UNIX system, you'll find a lot of tty files connected to virtual consoles (e.g. tty1 on linux), virtual terminals (e.g. pts/0) or physically connected hardware (e.g. ttyS0 is the physical serial terminal, if any, attached on first serial port of the host).
A console must be a piece of hardware physically connected to (or part of) the host. It has a special role in the system: it is the main point to access a system for maintenance and some special operation can be done only from a console (e.g. see single user mode). A terminal can be, and usually is, a remote piece of hardware.
Last, but not the least, a shell is a special program that interacts with a user through a controlling ttyand offers, to the user, the way of launching other programs (e.g. bash, csh, tcsh).
A terminal emulator is a program that emulates a physical terminal (e.g. xterm, gnome-terminal, minicom).
So when you look to a "text window" on your linux system (under X11) you are looking to: a terminal emulator, connected to a virtual terminal, identified by a tty file, inside which runs a shell.
(三)
控制台(Console)
控制台(Console)是物理设备,用于输入输出,它直接连接在计算机上,是计算机系统的一部分。计算机输出的信息会显示在控制台上,例如BIOS的输出,内核的输出。
终端(Terminal)
终端(Terminal)也是一台物理设备,只用于输入输出,本身没有强大的计算能力。一台计算机只有一个控制台,在计算资源紧张的时代,人们想共享一台计算机,可以通过终端连接到计算机上,将指令输入终端,终端传送给计算机,计算机完成指令后,将输出传送给终端,终端将结果显示给用户。
虚拟控制台(Virtual Console),虚拟终端(Virtual Terminal)
虚拟控制台(Virtual Console)和虚拟终端是一样的。我们只有一台终端(物理设备),这是我们与计算机之间的用户接口。假如有一天,我们想拥有多个用户接口,那么,一方面我们可以增加终端数目(物理设备),另一方面,还可以在同一台终端(物理设备)上虚拟出多个终端,它们之间互相不影响,至少看起来互相不影响。这些终端就是虚拟终端。
在Ubuntu中,我们按下Ctrl+Alt+Fx时,会进入第x个虚拟终端,一共有七个虚拟终端,其中第七个虚拟终端,就是我们默认使用的图形用户界面。
终端模拟器(Terminal Emulator)
我们知道,终端是一种物理设备,而终端模拟器(Terminal Emulator),是一个程序,这些程序用来模拟物理终端。图形用户界面中的终端模拟器一般称为终端窗口(Terminal Window),我们在Ubuntu下打开的gnome-terminal就属于此类。
tty
tty的全称是TeleTYpewriter,这就是早期的终端(物理设备),它们用于向计算机发送数据,并将计算机的返回结果打印出来。显示器出现后,终端不再将结果打印出来,而是显示在显示器上。但是tty的名字还是保留了下来。
在Ubuntu中,我们按下Ctrl+Alt+F1时,会进入第1个虚拟终端,你可以看到屏幕上方显示的tty1。
shell
shell 和之前说的几个概念截然不同,之前的几个概念都是与计算机的输入输出相关的,而shell是和内核相关的。内核为上层的应用提供了很多服务,shell在内核的上层,在应用程序的下层。例如,你写了一个 hello world 程序,你并不用显式地创建一个进程来运行你的程序,你把写好的程序交给shell就行了,由shell负责为你的程序创建进程。
我们在终端模拟器中输入命令时,终端模拟器本身并不解释执行这些命令,它只负责输入输出,真正解释执行这些命令的,是shell。
我们平时使用的sh, bash, csh是shell的不同实现。
-
sh sh这个概念本身就有岐义,它可以指shell程序的名字,也代表了shell的实现。
Thompson shell是第一个Unix shell,由 Ken Thompso于1971年在Unix第一版本中引入,shell的程序名即为sh。Bourne shell作为Thompson shell的替代,由 Stephen Bourne于1977年在Unix第七版中引入,它的程序名也是sh。Bourne shell不仅仅是一个命令解释器,更作为一种编程语言,提供了Thompson shell不具备的程序控制功能,并随着 Brian W. Kernighan 和 Rob Pike 的 The UNIX Programming Environment的出版而名声大噪。
-
csh csh全称为 C Shell,由 Bill Joy在70年代晚期完成,那时候他还是加州伯克利大学的研究生。tcsh是csh的升级版。与sh不同,csh的shell脚本,语法接近于C语言。
-
bash bash是由 Brian Fox为GNU项目开发的自由软件,作为Bourne shell的替代品,于1989年发布。是Linux和Mac OS X的默认shell。bash的命令语法是Bourne shell命令语法的超集,从ksh和csh借鉴了一些思想。
好了,就写到这里,上面的内容是我参考维基百科后写下的,不保证完全正确, 下面还提供了一些资料,如果有兴趣可以阅读一下。
浙公网安备 33010602011771号