一、简介

Shell 是计算机操作系统中的一种用户界面,允许用户与操作系统进行交互。它是一个命令解释器,接受用户的命令并将其转化为操作系统能够理解的指令,然后执行这些指令。Shell 可以提供一个命令行界面(Command-Line Interface,CLI)或者图形用户界面(Graphical User Interface,GUI)。

在 Unix、Linux 和类 Unix 系统中,Shell 通常是一个用户与内核(操作系统的核心)之间的接口,是链接内核和应用程序的桥梁。用户可以通过 Shell 输入命令,Shell 将这些命令解释并将它们传递给操作系统内核执行。

Shell 还可以执行脚本,这是一系列按顺序排列的命令,这样用户就可以通过运行脚本自动执行一系列任务。

一些常见的 Shell 包括:

  • Bash(Bourne Again SHell):是 Unix 和 Linux 系统中最常见的 Shell,也是许多系统的默认 Shell。
  • Zsh(Z shell):也是一种强大的 Shell,相对于 Bash 来说,提供了更多的特性和可定制性。
  • Fish:是一种用户友好的 Shell,具有语法高亮、智能补全等特性。
  • PowerShell:是由 Microsoft 开发的用于 Windows 的 Shell,具有强大的脚本编程能力和对象导向的特性。

二、GNU 官方解释

在 GNU 软件官方对 Bash 的介绍中有一个章节介绍的是什么是 Shell

1、详细介绍

At its base, a shell is simply a macro processor that executes commands. The term macro processor means functionality where text and symbols are expanded to create larger expressions.

A Unix shell is both a command interpreter and a programming language. As a command interpreter, the shell provides the user interface to the rich set of GNU utilities. The programming language features allow these utilities to be combined. Files containing commands can be created, and become commands themselves. These new commands have the same status as system commands in directories such as /bin, allowing users or groups to establish custom environments to automate their common tasks.

Shells may be used interactively or non-interactively. In interactive mode, they accept input typed from the keyboard. When executing non-interactively, shells execute commands read from a file.

A shell allows execution of GNU commands, both synchronously and asynchronously. The shell waits for synchronous commands to complete before accepting more input; asynchronous commands continue to execute in parallel with the shell while it reads and executes additional commands. The redirection constructs permit fine-grained control of the input and output of those commands. Moreover, the shell allows control over the contents of commands’ environments.

Shells also provide a small set of built-in commands (builtins) implementing functionality impossible or inconvenient to obtain via separate utilities. For example, cdbreakcontinue, and exec cannot be implemented outside of the shell because they directly manipulate the shell itself. The historygetoptskill, or pwd builtins, among others, could be implemented in separate utilities, but they are more convenient to use as builtin commands. All of the shell builtins are described in subsequent sections.

While executing commands is essential, most of the power (and complexity) of shells is due to their embedded programming languages. Like any high-level language, the shell provides variables, flow control constructs, quoting, and functions.

Shells offer features geared specifically for interactive use rather than to augment the programming language. These interactive features include job control, command line editing, command history and aliases. Each of these features is described in this manual.

在基本层面上,shell 只是一个执行命令的宏处理器。宏处理器这个术语指的是将文本和符号扩展为更大表达式的功能。

Unix shell 既是命令解释器,又是一种编程语言。作为命令解释器,shell 为用户提供了丰富的 GNU 实用程序集的用户界面。编程语言功能允许组合这些实用程序。可以创建包含命令的文件,并使其成为命令本身。这些新命令在 /bin 等目录中具有与系统命令相同的状态,允许用户或组创建自定义环境以自动执行常见任务。

shell 可以以交互或非交互方式使用。在交互模式下,它们接受从键盘输入的命令。在执行非交互方式时,shell 会执行从文件中读取的命令。

shell 允许同步和异步地执行 GNU 命令。shell 在等待同步命令完成之前会接受更多输入;而异步命令在 shell 读取和执行其他命令的同时继续并行执行。重定向构造允许对这些命令的输入和输出进行精细控制。此外,shell 允许控制命令环境的内容。

shell 还提供了一小组内置命令(builtins),实现通过单独实用程序无法获得或不方便获得的功能。例如,cdbreakcontinueexec 不能在 shell 之外实现,因为它们直接操纵 shell 本身。historygetoptskillpwd 等内置命令可以在单独的实用程序中实现,但作为内置命令使用更为方便。所有 shell 内置命令在随后的部分中进行了描述。

虽然执行命令是必不可少的,但 shell 的大部分力量(和复杂性)来自于其嵌入式编程语言。与任何高级语言一样,shell 提供变量、流程控制结构、引用和函数。

shell 提供的功能主要面向交互使用,而不是增强编程语言。这些交互功能包括作业控制、命令行编辑、命令历史和别名。这些功能在本手册中都有描述。

相关链接

什么是Bash? - z2huo

软件 - GNU 工程 - 自由软件基金会

Bash - GNU Project - Free Software Foundation

Bash Reference Manual (gnu.org)

[[什么是 Bash?]]

OB tags

#Linux #Shell