命令行解釋器

出自 Arch Linux 中文维基

根據 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 之間的共同的配置。

請參閱相關文章相關存儲庫,也請參閱xsh

輸入與輸出[編輯 | 編輯原始碼]

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
    one
    two
    three
    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.

參見[編輯 | 編輯原始碼]