来自 Arch Linux 中文维基
(重定向自Command-line shell

根据 Wikipedia:

Unix shell 是为 Unix 和类 Unix 系统提供传统用户界面的命令行解释器或 shell,通过执行用户输入的命令文本,或包含命令的文本脚本来指导计算机的运行。

Shell 列表[编辑 | 编辑源代码]

或多或少符合 POSIX 的 Shell 列在 #符合 POSIX 下,语法不同的 Shell 列在 #其他 Shell 下。

符合 POSIX[编辑 | 编辑源代码]

可以从 /usr/bin/sh 链接到这些 Shell。当使用 sh 名称调用 BashmkshAURzsh 时,它们会自动变得更符合 POSIX。

  • Bash — Bash 扩展了 Bourne shell,支持命令行历史和补全、索引和关联数组、整数算术、进程替换、here 字符串、正则表达式匹配和大括号展开。
https://www.gnu.org/software/bash/ || bash
  • Dash — Almquist SHell (ash) 的 NetBSD 版本的后代。快速且符合 POSIX 的 Shell,以尽可能小为目标。
http://gondor.apana.org.au/~herbert/dash/ || dash
  • KornShell(ksh) — KornShell 语言是完整、强大的高级编程语言,通常比其他高级语言更容易快捷。这使得它特别适合于原型开发。Ksh 拥有 Bourne shell 和 C shell 的最佳特性,以及独有的许多新特性。因此,无论是系统交互,还是编程,ksh 都可以大大提高工作效率和质量。Ksh 程序更容易编写,并且比用 C 等低级语言编写的程序更简洁和可读。
http://www.kornshell.com || ksh
  • nsh — 类似于 Fish 的命令行 shell,但兼容 POSIX。
https://github.com/nuta/nsh || nsh-gitAUR
  • Oil Shell(OSH) — Oil Shell 是兼容 Bash 的 UNIX 命令行 Shell。OSH 可以在大多数类 UNIX 操作系统上运行,包括 GNU/Linux。它是用 Python (v2.7) 编写的,但附带了一个原生可执行文件。OSH 使用的 Bash 方言称为 OSH 语言。
https://www.oilshell.org || oil
  • Yash — Yet another shell(另一个 shell)是用 C99(ISO/IEC 9899:1999)编写的符合 POSIX 的命令行 shell。Yash 旨在成为最符合 POSIX 的 shell,同时支持日常交互和脚本使用。
https://yash.osdn.jp || yashAUR
  • Zsh — 为交互式使用而设计的 Shell,同时也是一门强大的脚本语言。Bash、ksh 和 tcsh 的许多有用特性都被合并到 Zsh 中,还添加了许多原创特性。介绍性文档详细介绍了 Zsh 的一些特性。
https://www.zsh.org/ || zsh
提示:可以用 shellcheck lint POSIX 和 Bash 脚本。

其他 Shell[编辑 | 编辑源代码]

  • C shell (tcsh) — Command language interpreter usable both as an interactive login shell and a shell script command processor. It includes a command-line editor, programmable word completion, spelling correction, a history mechanism, job control and a C-like syntax.
https://www.tcsh.org || tcsh
  • Closh — Bash-like shell based on Clojure.
https://github.com/dundalek/closh || closhAUR
  • Elvish — Elvish is a modern and expressive shell, that can carry internal structured values through pipelines. This feature makes possible avoiding a lot of complex text processing code. It features an expressive programming language, with features like exceptions, namespacing and anonymous functions. It also has a powerful readline which checks the syntax while typing, and syntax highlighting by default.
https://elv.sh || elvish
  • fish — Smart and user-friendly command line shell. Fish performs full-color command line syntax highlighting, as well as highlighting and completion for commands and their arguments, file existence, and history. It supports complete-as-you-type for history and commands. Fish is able to parse the system's man pages in order to determine valid arguments for commands, allowing it to highlight and complete commands. Easy last-command revision can be done using Alt+Up. The fish daemon (fishd) facilitates synchronized history across all instances of fish, as well as universal and persistent environment variables. Additionally, fish features significantly simplified programming syntax and control flow (similar to ruby). For more information, see the tutorial.
https://fishshell.com/ || fish
  • ion — Ion is a modern system shell that features a simple, yet powerful, syntax. It is written entirely in Rust, which greatly increases the overall quality and security of the shell, eliminating the possibilities of a ShellShock-like vulnerability, and making development easier. It also offers a level of performance that exceeds that of Dash, when taking advantage of Ion's features. While it is developed alongside, and primarily for, RedoxOS, it is a fully capable on other *nix platforms. For more details lookup its manual.
https://gitlab.redox-os.org/redox-os/ion/ || ion-gitAUR
  • Murex — A command line shell with improved features and an enhanced UX.
https://murex.rocks/ || murexAUR
  • Nash — Nash is a system shell, inspired by plan9 rc, that makes it easy to create reliable and safe scripts taking advantages of operating systems namespaces (on Linux and plan9) in an idiomatic way.
https://github.com/NeowayLabs/nash || nash-gitAUR
  • nushell — Nu draws inspiration from functional programming languages, and modern CLI tools. Rather than thinking of files and services as raw streams of text, Nu looks at each input as something with structure.
https://www.nushell.sh || nushell
  • Oh — Unix shell written in Go. It is similar in spirit but different in detail from other Unix shells. Oh extends the shell's programming language features without sacrificing the shell's interactive features.
https://github.com/michaelmacinnis/oh || oh-gitAUR
  • PowerShell — PowerShell is an object-oriented programming language and interactive command line shell, originally written for and exclusive to Windows. Later on, it was open sourced and ported to macOS and Linux.
https://github.com/PowerShell/PowerShell || powershellAUR
  • rc — Command interpreter for Plan 9 that provides similar facilities to UNIX’s Bourne shell, with some small additions and less idiosyncratic syntax.
http://doc.cat-v.org/plan_9/4th_edition/papers/rc || 9base
  • xonsh — Python-powered shell with additional shell primitives that you are used to from Bash and IPython.
https://xon.sh/ || xonsh

更改默认 Shell[编辑 | 编辑源代码]

在安装上面的 Shell 之后,运行其可执行文件即可在当前 Shell 中直接执行。但是,如果想在登录时直接使用,则需要更改默认 Shell。

列出所有已安装的 shell:

$ chsh -l

为当前用户设置默认 Shell:

$ chsh -s /full/path/to/shell

如果使用 systemd-homed,请运行:

$ homectl update --shell=/full/path/to/shell 用户名

其中 /full/path/to/shellchsh -l 提供的完整路径。

注意: The default shell for new users is Bash. To switch back to it, use /usr/bin/bash as the shell path in the above commands.

如果现在注销并再次登录,就将使用新设置的 Shell。

提示:chsh 会参考 /etc/shells。如果最近安装的 Shell 不在列表中,可以手动将其添加到此文件中。

卸载 Shell[编辑 | 编辑源代码]

警告: 不遵循以下步骤可能会导致用户无法访问能用的 Shell。

移除 Shell 软件包前先更改默认 Shell

或者,修改用户数据库英语user database

对于每个设置了除 bash 之外的 Shell 作为登录 Shell 的用户(如果有必要,也包括 root 用户)执行这一操作。完成后就可以移除软件包。

登录 Shell[编辑 | 编辑源代码]

登录 Shell 是一种调用模式,在这种模式下,Shell 读取一次性初始化文件,例如系统范围的 /etc/profile、用户的 ~/.profile 或其他特定于 Shell 的文件。这些文件将会设置初始环境,且会被所有从 Shell 启动的进程(包括其他非登录 Shell 或图形程序)继承。因此,其只会在会话开始时读取一次,例如,在控制台或通过 SSH 登录、使用 --login 参数通过 sudosu 切换用户、手动调用登录 Shell(例如,通过 bash --login)。

可在#配置文件及其所包含的链接中查看各种初始化文件的概述。参见 Stackexchange 上的登录 Shell 和非登录 Shell 之间的区别?(英文)为什么要用“登录”Shell 而不是“非登录”Shell?(英文)了解更多有关登录 Shell 的信息.

配置文件[编辑 | 编辑源代码]

要在登录时或在控制台中自动启动程序,可以使用 Shell 启动文件或目录。阅读 Shell 的文档或其对应文章,例如 Bash#配置文件Zsh#启动/结束文件英语Zsh#Startup/Shutdown files

Wikipedia:Unix shell#Configuration files 中有对各种 Shell 配置文件的比较。

/etc/profile[编辑 | 编辑源代码]

在登录时,所有兼容 Bourne Shell 的 Shell 都会读取 /etc/profile,而 /etc/profile 又会读取 /etc/profile.d/ 中任何可读的 *.sh 文件:这些脚本不需要解释器指令,也不需要具有可执行权限。它们用于设置环境并定义特定于应用程序的设置。

标准化[编辑 | 编辑源代码]

可以使(某些)Shell 配置文件遵循相同的命名约定,并支持一些 Shell 之间的共同的配置。


输入与输出[编辑 | 编辑源代码]

See also GregsWiki and I/O Redirection.

  • Redirections truncate files before commands are executed:
    $ command file > file
    will therefore not work as expected. While some commands (sed for example) provide an option to edit files in-place, many do not. In such cases you can use the sponge(1) command from the moreutils package.
  • Because cat is not built into the shell, on many occasions you may find it more convenient to use a redirection, for example in scripts, or if you care a lot about performance. In fact < file does the same as cat file.
  • POSIX-compliant shells support Here Documents:
    $ cat << EOF
  • Shell pipelines operate on stdout by default. To operate on stderr(3) you can redirect stderr to stdout with command 2>&1 | othercommand or, for Bash 4, command |& othercommand.
  • Remember that many GNU core utilities accept files as arguments, so for example grep pattern < file is replaceable with grep pattern file.

参见[编辑 | 编辑源代码]