sh 和 bash 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5725296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:26:38  来源:igfitidea点击:

Difference between sh and bash

bashshellunixsh

提问by Weiwei Yang

When writing shell programs, we often use /bin/shand /bin/bash. I usually use bash, but I don't know what's the difference between them.

在编写 shell 程序时,我们经常使用/bin/sh/bin/bash。我通常使用bash,但我不知道它们之间有什么区别。

What's main difference between bashand sh?

bash和之间的主要区别是sh什么?

What do we need to be aware of when programming in bashand sh?

bash和 中编程时我们需要注意sh什么?

回答by Roman Cheplyaka

What is sh

什么是sh

sh(or the Shell Command Language) is a programming language described by the POSIX standard. It has many implementations (ksh88, dash, ...). bashcan also be considered an implementation of sh(see below).

sh(或 Shell 命令语言)是一种由POSIX 标准描述的编程语言。它有许多实现(ksh88, dash, ...)。bash也可以被认为是sh(见下文)的实现。

Because shis a specification, not an implementation, /bin/shis a symlink (or a hard link) to an actual implementation on most POSIX systems.

因为sh是规范,而不是实现,/bin/sh是大多数 POSIX 系统上实际实现的符号链接(或硬链接)。

What is bash

什么是bash

bashstarted as an sh-compatible implementation (although it predates the POSIX standard by a few years), but as time passed it has acquired many extensions. Many of these extensions may change the behavior of valid POSIX shell scripts, so by itself bashis not a valid POSIX shell. Rather, it is a dialect of the POSIX shell language.

bash一开始是一个sh兼容的实现(尽管它比 POSIX 标准早了几年),但随着时间的推移,它已经获得了许多扩展。许多这些扩展可能会改变有效 POSIX shell 脚本的行为,因此它本身bash不是有效的 POSIX shell。相反,它是 POSIX shell 语言的一种方言。

bashsupports a --posixswitch, which makes it more POSIX-compliant. It also tries to mimic POSIX if invoked as sh.

bash支持--posix开关,使其更符合 POSIX。如果作为sh.

sh = bash?

sh = 重击?

For a long time, /bin/shused to point to /bin/bashon most GNU/Linux systems. As a result, it had almost become safe to ignore the difference between the two. But that started to change recently.

长期以来,/bin/sh用于指向/bin/bash大多数 GNU/Linux 系统。结果,忽略两者之间的差异几乎变得安全。但最近这种情况开始发生变化。

Some popular examples of systems where /bin/shdoes not point to /bin/bash(and on some of which /bin/bashmay not even exist) are:

/bin/sh不指向/bin/bash(其中一些/bin/bash甚至可能不存在)的系统的一些流行示例是:

  1. Modern Debian and Ubuntu systems, which symlink shto dashby default;
  2. Busybox, which is usually run during the Linux system boot time as part of initramfs. It uses the ashshell implementation.
  3. BSDs, and in general any non-Linux systems. OpenBSD uses pdksh, a descendant of the Korn shell. FreeBSD's shis a descendant of the original UNIX Bourne shell. Solaris has its own shwhich for a long time was not POSIX-compliant; a free implementation is available from the Heirloom project.
  1. 现代Debian和Ubuntu系统,其符号链接shdash默认;
  2. Busybox,它通常在 Linux 系统启动时作为initramfs. 它使用ashshell实现。
  3. BSD,以及通常的任何非 Linux 系统。OpenBSD 使用pdksh,Korn shell 的后代。FreeBSDsh是原始 UNIX Bourne shell 的后代。Solaris 有它自己的sh,它在很长一段时间内都不是 POSIX 兼容的;传家宝项目提供了一个免费的实现。

How can you find out what /bin/shpoints to on your system?

如何找出/bin/sh系统上的指向?

The complication is that /bin/shcould be a symbolic link or a hard link. If it's a symbolic link, a portableway to resolve it is:

复杂的是这/bin/sh可能是符号链接或硬链接。如果它是符号链接,解决它的可移植方法是:

% file -h /bin/sh
/bin/sh: symbolic link to bash

If it's a hard link, try

如果是硬链接,请尝试

% find -L /bin -samefile /bin/sh
/bin/sh
/bin/bash

In fact, the -Lflag covers both symlinks and hardlinks, but the disadvantage of this method is that it is not portable — POSIX does not requirefindto support the -samefileoption, although both GNU findand FreeBSD findsupport it.

事实上,该-L标志涵盖了符号链接和硬链接,但这种方法的缺点是不可移植——POSIX不需要find支持该-samefile选项,尽管GNU findFreeBSD find 都支持它。

Shebang line

社帮线

Ultimately, it's up to you to decide which one to use, by writing the ?shebang? line as the very first line of the script.

最终,由您决定使用哪个,通过编写 ?shebang? line 作为脚本的第一行。

E.g.

例如

#!/bin/sh

will use sh(and whatever that happens to point to),

将使用sh(以及任何碰巧指向的东西),

#!/bin/bash

will use /bin/bashif it's available (and fail with an error message if it's not). Of course, you can also specify another implementation, e.g.

/bin/bash在可用时使用(如果不可用则失败并显示错误消息)。当然,你也可以指定另一种实现,例如

#!/bin/dash

Which one to use

使用哪一个

For my own scripts, I prefer shfor the following reasons:

对于我自己的脚本,我更喜欢sh以下原因:

  • it is standardized
  • it is much simpler and easier to learn
  • it is portable across POSIX systems — even if they happen not to have bash, they are required to have sh
  • 它是标准化的
  • 它更简单,更容易学习
  • 它在 POSIX 系统之间是可移植的——即使他们碰巧没有bash,他们也必须有sh

There are advantages to using bashas well. Its features make programming more convenient and similar to programming in other modern programming languages. These include things like scoped local variables and arrays. Plain shis a very minimalistic programming language.

使用也有好处bash。它的特性使编程更加方便,并且类似于其他现代编程语言中的编程。这些包括范围局部变量和数组之类的东西。Plainsh是一种非常简约的编程语言。

回答by Rein Henrichs

sh: http://man.cx/sh
bash: http://man.cx/bash

shhttp
bash: //man.cx/shhttp: //man.cx/bash

TL;DR: bashis a superset of shwith a more elegant syntax and more functionality. It is safe to use a bash shebang line in almost all cases as it's quite ubiquitous on modern platforms.

TL;DRbashsh具有更优雅语法和更多功能的超集。在几乎所有情况下使用 bash shebang 行都是安全的,因为它在现代平台上非常普遍。

NB: in some environments, shisbash. Check sh --version.

注意:在某些环境中,shbash. 检查sh --version

回答by tripleee

This question has frequently been nominated as a canonical for people who try to use shand are surprised that it's not behaving the same as bash. Here's a quick rundown of common misunderstandings and pitfalls.

对于尝试使用sh并惊讶于它的行为与bash. 以下是常见误解和陷阱的简要概述。

First off, you should understand what to expect.

首先,您应该了解会发生什么。

  • If you run your script with sh scriptname, or run it with scriptnameand have #!/bin/shin the shebangline, you should expect POSIX shbehavior.
  • If you run your script with bash scriptname, or run it with scriptnameand have #!/bin/bash(or the local equivalent) in the shebang line, you should expect Bash behavior.
  • 如果您使用 运行您的脚本sh scriptname,或者使用scriptname#!/bin/shshebang行中运行它,您应该期待POSIXsh行为。
  • 如果您使用 运行您的脚本bash scriptname,或者使用scriptname#!/bin/bash在 shebang 行中使用(或本地等效项)运行它,您应该期待 Bash 行为。

Having a correct shebang and running the script by typing just the script name (possibly with a relative or full path) is generally the preferred solution. In addition to a correct shebang, this requires the script file to have execute permission (chmod a+x scriptname).

拥有正确的shebang 并通过仅键入脚本名称(可能带有相对路径或完整路径)来运行脚本通常是首选的解决方案。除了正确的shebang,这还需要脚本文件具有执行权限( chmod a+x scriptname)。

So, how do they actually differ?

那么,它们实际上有何不同?

The Bash Reference manual has a section which attempts to enumerate the differencesbut some common sources of confusion include

Bash 参考手册有一个部分试图列举差异,但一些常见的混淆来源包括

  • [[is not available in sh(only [which is more clunky and limited).
  • shdoes not have arrays.
  • Some Bash keywords like local, source, function, shopt, let, declare, and selectare not portable to sh. (Some shimplementations support e.g. local.)
  • Bash has many C-style syntax extensions like the three-argument for((i=0;i<=3;i++))loop, +=increment assignment, etc. The $'string\nwith\tC\aescapes'feature is tentatively accepted for POSIX(meaning it works in Bash now, but will not yet be supported by shon systems which only adhere to the current POSIX specification, and likely will not for some time to come).
  • Bash supports <<<'here strings'.
  • Bash has *.{png,jpg}and {0..12}brace expansion.
  • ~refers to $HOMEonly in Bash (and more generally ~usernameto the home directory of username).This is in POSIX, but may be missing from some pre-POSIX /bin/shimplementations.
  • Bash has process substitution with <(cmd)and >(cmd).
  • Bash has Csh-style convenience redirection aliases like &|for 2>&1 |and &>for > ... 2>&1
  • Bash supports coprocesses with <>redirection.
  • Bash features a rich set of expanded non-standard parameter expansions such as ${substring:1:2}, ${variable/pattern/replacement}, case conversion, etc.
  • Bash has significantly extended facilities for shell arithmetic (though still no floating-point support). There is an obsolescent legacy $[expression]syntax which however should be replaced with POSIX arithmetic $((expression))syntax. (Some legacy pre-POSIX shimplementations may not support that, though.)
  • Magic variables like $RANDOM, $SECONDS, $PIPESTATUS[@]and $FUNCNAMEare Bash extensions.
  • Syntactic differences like export variable=valueand [ "x" == "y" ]which are not portable (export variableshould be separate from variable assignment, and portable string comparison in [ ... ]uses a single equals sign).
  • Many, many Bash-only extensions to enable or disable optional behavior and expose internal state of the shell.
  • Many, many convenience features for interactive use which however do not affect script behavior.
  • [[不可用sh(只有[更笨重和有限的)。
  • sh没有数组。
  • 有些猛砸的关键字时localsourcefunctionshoptletdeclare,和select无法移植到sh。(一些sh实现支持例如local。)
  • Bash 有许多 C 风格的语法扩展,如三参数for((i=0;i<=3;i++))循环、+=增量赋值等。 该$'string\nwith\tC\aescapes'功能暂时被 POSIX 接受(这意味着它现在可以在 Bash 中使用,但sh在仅遵守当前规则的系统上尚不支持)POSIX 规范,并且可能在未来一段时间内不会)。
  • Bash 支持<<<'here strings'.
  • Bash有*.{png,jpg}{0..12}括号扩展。
  • ~$HOME仅在 Bash中指代(更一般~username地指的是的主目录username)。这在 POSIX 中,但在某些 POSIX 之前的/bin/sh实现中可能会丢失。
  • Bash 使用<(cmd)和 进行进程替换>(cmd)
  • Bash 具有 Csh 风格的方便重定向别名,例如&|for2>&1 |&>for> ... 2>&1
  • Bash 支持带有<>重定向的协进程。
  • Bash 具有丰富的扩展非标准参数扩展集,例如${substring:1:2}${variable/pattern/replacement}、大小写转换等。
  • Bash 显着扩展了 shell 算法的功能(尽管仍然没有浮点支持)。有一个过时的遗留$[expression]语法,但是应该用 POSIX 算术$((expression))语法替换。(不过,一些旧的 POSIX 之前的sh实现可能不支持。)
  • 魔术变量,如$RANDOM$SECONDS$PIPESTATUS[@]$FUNCNAME是Bash的扩展。
  • export variable=value[ "x" == "y" ]不可移植的语法差异(export variable应该与变量赋值分开,并且可移植字符串比较[ ... ]使用单个等号)。
  • 许多很多 Bash-only 扩展用于启用或禁用可选行为并公开 shell 的内部状态。
  • 然而,许多交互使用的便利功能不会影响脚本行为。

Remember, this is an abridged listing. Refer to the reference manual for the full scoop, and http://mywiki.wooledge.org/Bashismfor many good workarounds; and/or try http://shellcheck.net/which warns for many Bash-only features.

请记住,这是一个删节列表。有关完整的独家新闻,请参阅参考手册,并参阅http://mywiki.wooledge.org/Bashism以了解许多好的解决方法;和/或尝试http://shellcheck.net/警告许多 Bash-only 功能。

A common error is to have a #!/bin/bashshebang line, but then nevertheless using sh scriptnameto actually run the script. This basically disables any Bash-only functionality, so you get syntax errors e.g. for trying to use arrays. (The shebang line is syntactically a comment, so it is simply ignored in this scenario.)

一个常见的错误是有一个#!/bin/bashshebang 行,但仍然使用它sh scriptname来实际运行脚本。这基本上禁用了任何 Bash-only 功能,因此您会收到语法错误,例如尝试使用数组。(shebang 行在语法上是一个注释,因此在这种情况下它被简单地忽略。)

Unfortunately, Bash will not warn when you try to use these constructs when it is invoked as sh. It doesn't completely disable allBash-only functionality, either, so running Bash by invoking it as shis not a good way to check if your script is properly portable to ash/dash/POSIX shor variants like Heirloom sh

不幸的是,当 Bash 被调用为sh. 它也不会完全禁用所有Bash-only 功能,因此通过调用 Bash 来运行 Bashsh并不是检查脚本是否可以正确移植到ash/ dash/POSIXshHeirloom 等变体的好方法sh

回答by Premraj

Shellis an interface between a user and OS to access to an operating system's services. It can be either GUI or CLI (Command Line interface).

Shell是用户和操作系统之间的接口,用于访问操作系统的服务。它可以是 GUI 或 CLI(命令行界面)。

sh(Bourne shell) is a shell command-line interpreter, for Unix/Unix-like operating systems. It provides some built-in commands. In scripting language we denote interpreter as #!/bin/sh. It was one most widely supported by other shells like bash (free/open), kash (not free).

sh(Bourne shell) 是一个 shell 命令行解释器,适用于 Unix/类 Unix 操作系统。它提供了一些内置命令。在脚本语言中,我们将解释器表示为#!/bin/sh。它是其他 shell 最广泛支持的一种,如 bash(免费/开放)、kash(非免费)。

Bash(Bourne again shell) is a shell replacement for the Bourne shell. Bash is superset of sh. Bash supports sh. POSIX is a set of standards defining how POSIX-compliant systems should work. Bash is not actually a POSIX compliant shell. In a scripting language we denote the interpreter as #!/bin/bash.

ourne一个增益小号地狱)是一个外壳更换对于Bourne壳。Bash 是 sh 的超集。Bash 支持 sh。POSIX 是一组标准,用于定义符合 POSIX 的系统应该如何工作。Bash 实际上不是 POSIX 兼容的 shell。在脚本语言中,我们将解释器表示为#!/bin/bash

Analogy:

比喻:

  • Shell is like an interface or specifications or API.
  • sh is a class which implements the Shell interface.
  • Bash is a subclass of the sh.
  • Shell 就像一个接口或规范或 API。
  • sh 是一个实现 Shell 接口的类。
  • Bash 是 sh 的子类。

enter image description here

在此处输入图片说明

回答by SriniV

Post from UNIX.COM

来自UNIX.COM 的帖子

Shell features

外壳功能

This table below lists most features that I think would make you choose one shell over another. It is not intended to be a definitive list and does not include every single possible feature for every single possible shell. A feature is only considered to be in a shell if in the version that comes with the operating system, or if it is available as compiled directly from the standard distribution. In particular the C shell specified below is that available on SUNOS 4.*, a considerable number of vendors now ship either tcsh or their own enhanced C shell instead (they don't always make it obvious that they are shipping tcsh.

下表列出了我认为会让您选择一种外壳而不是另一种外壳的大多数功能。它不是一个明确的列表,也不包括每个可能的 shell 的每个可能的功能。只有在操作系统随附的版本中,或者如果直接从标准发行版编译可用,则功能才被视为在 shell 中。特别是下面指定的 C shell 在 SUNOS 4.* 上可用,相当多的供应商现在提供 tcsh 或他们自己的增强型 C shell(他们并不总是明确表示他们正在提供 tcsh。

Code:

代码:

                                     sh   csh  ksh  bash tcsh zsh  rc   es
Job control                          N    Y    Y    Y    Y    Y    N    N
Aliases                              N    Y    Y    Y    Y    Y    N    N
Shell functions                      Y(1) N    Y    Y    N    Y    Y    Y
"Sensible" Input/Output redirection  Y    N    Y    Y    N    Y    Y    Y
Directory stack                      N    Y    Y    Y    Y    Y    F    F
Command history                      N    Y    Y    Y    Y    Y    L    L
Command line editing                 N    N    Y    Y    Y    Y    L    L
Vi Command line editing              N    N    Y    Y    Y(3) Y    L    L
Emacs Command line editing           N    N    Y    Y    Y    Y    L    L
Rebindable Command line editing      N    N    N    Y    Y    Y    L    L
User name look up                    N    Y    Y    Y    Y    Y    L    L
Login/Logout watching                N    N    N    N    Y    Y    F    F
Filename completion                  N    Y(1) Y    Y    Y    Y    L    L
Username completion                  N    Y(2) Y    Y    Y    Y    L    L
Hostname completion                  N    Y(2) Y    Y    Y    Y    L    L
History completion                   N    N    N    Y    Y    Y    L    L
Fully programmable Completion        N    N    N    N    Y    Y    N    N
Mh Mailbox completion                N    N    N    N(4) N(6) N(6) N    N
Co Processes                         N    N    Y    N    N    Y    N    N
Builtin artithmetic evaluation       N    Y    Y    Y    Y    Y    N    N
Can follow symbolic links invisibly  N    N    Y    Y    Y    Y    N    N
Periodic command execution           N    N    N    N    Y    Y    N    N
Custom Prompt (easily)               N    N    Y    Y    Y    Y    Y    Y
Sun Keyboard Hack                    N    N    N    N    N    Y    N    N
Spelling Correction                  N    N    N    N    Y    Y    N    N
Process Substitution                 N    N    N    Y(2) N    Y    Y    Y
Underlying Syntax                    sh   csh  sh   sh   csh  sh   rc   rc
Freely Available                     N    N    N(5) Y    Y    Y    Y    Y
Checks Mailbox                       N    Y    Y    Y    Y    Y    F    F
Tty Sanity Checking                  N    N    N    N    Y    Y    N    N
Can cope with large argument lists   Y    N    Y    Y    Y    Y    Y    Y
Has non-interactive startup file     N    Y    Y(7) Y(7) Y    Y    N    N
Has non-login startup file           N    Y    Y(7) Y    Y    Y    N    N
Can avoid user startup files         N    Y    N    Y    N    Y    Y    Y
Can specify startup file             N    N    Y    Y    N    N    N    N
Low level command redefinition       N    N    N    N    N    N    N    Y
Has anonymous functions              N    N    N    N    N    N    Y    Y
List Variables                       N    Y    Y    N    Y    Y    Y    Y
Full signal trap handling            Y    N    Y    Y    N    Y    Y    Y
File no clobber ability              N    Y    Y    Y    Y    Y    N    F
Local variables                      N    N    Y    Y    N    Y    Y    Y
Lexically scoped variables           N    N    N    N    N    N    N    Y
Exceptions                           N    N    N    N    N    N    N    Y

Key to the table above.

上表的关键。

Y Feature can be done using this shell.

Y 功能可以使用此外壳完成。

N Feature is not present in the shell.

N 特征不存在于外壳中。

F Feature can only be done by using the shells function mechanism.

F Feature 只能通过使用shells 函数机制来完成。

L The readline library must be linked into the shell to enable this Feature.

L readline 库必须链接到shell 才能启用此功能。

Notes to the table above

上表注意事项

1. This feature was not in the original version, but has since become
   almost standard.
2. This feature is fairly new and so is often not found on many
   versions of the shell, it is gradually making its way into
   standard distribution.
3. The Vi emulation of this shell is thought by many to be
   incomplete.
4. This feature is not standard but unofficial patches exist to
   perform this.
5. A version called 'pdksh' is freely available, but does not have
   the full functionality of the AT&T version.
6. This can be done via the shells programmable completion mechanism.
7. Only by specifying a file via the ENV environment variable.

回答by Timothy L.J. Stewart

TERMINAL

终端

  • program(s) that put a window up
  • xterm, rxvt, konsole, kvt, gnome-terminal, nxterm, and eterm.
  • 安装窗口的程序
  • xterm、rxvt、konsole、kvt、gnome-terminal、nxterm 和 eterm。

SHELL

贝壳

  • Is a program that runs in the terminal
  • Shell is both a command interpreter and a programming language
  • Shell is simply a macro processor that executes commands.
  • Macro processor means functionality where text and symbols are expanded to create larger expressions.
  • 是在终端运行的程序
  • Shell既是命令解释器又是编程语言
  • Shell 只是一个执行命令的宏处理器。
  • 宏处理器是指扩展文本和符号以创建更大表达式的功能。

SH Vs. BASH

SH 对比。巴什

SH

上海

  • (SHell)
  • Is a specific shell
  • a command interpreter and a programming language
  • Predecessor of BASH
  • (贝壳)
  • 是特定的shell
  • 命令解释器和编程语言
  • BASH的前身

BASH

巴什

  • (Bourne-Again SHell)
  • Is a specific shell
  • a command interpreter and a programming language
  • Has sh functionality and more
  • Successor of SH
  • BASH is the default SHELL
  • (Bourne-Again SHell)
  • 是特定的shell
  • 命令解释器和编程语言
  • 具有 sh 功能等
  • SH的继任者
  • BASH 是默认的 SHELL

REFERENCE MATERIAL:

参考资料:

SHELLgnu.org:

壳牌gnu.org:

At its base, a shell is simply a macro processorthat 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, cd, break, continue, and exec cannot be implemented outside of the shellbecause they directly manipulate the shell itself. The history, getopts, kill, 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),实现了通过单独的实用程序不可能或不方便获得的功能例如,cd、break、continue 和 exec 不能在 shell 之外实现,因为它们直接操作 shell 本身。history、getopts、kill 或 pwd 等内置命令可以在单独的实用程序中实现,但作为内置命令使用它们更方便。所有的 shell 内置函数都在后续章节中描述。

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

Shell 提供专门用于交互使用的功能,而不是增强编程语言。这些交互功能包括作业控制、命令行编辑、命令历史记录和别名。本手册中介绍了这些功能中的每一个。

BASHgnu.org:

BASH gnu.org:

Bash is the shell, or command language interpreter, for the GNU operating system. The name is an acronym for the ‘Bourne-Again SHell', a pun on Stephen Bourne, the author of the direct ancestor of the current Unix shell sh, which appeared in the Seventh Edition Bell Labs Research version of Unix.

Bash is largely compatible with sh and incorporates useful features from the Korn shell ksh and the C shell csh. It is intended to be a conformant implementation of the IEEE POSIX Shell and Tools portion of the IEEE POSIX specification (IEEE Standard 1003.1). It offers functional improvements over sh for both interactive and programming use.

While the GNU operating system provides other shells, including a version of csh, Bash is the default shell. Like other GNU software, Bash is quite portable. It currently runs on nearly every version of Unix and a few other operating systems - independently-supported ports exist for MS-DOS, OS/2, and Windows platforms.

Bash 是 GNU 操作系统的 shell,或命令语言解释器。这个名字是“Bourne-Again SHell”的首字母缩写,是对斯蒂芬·伯恩的双关语,他是当前 Unix shell sh 的直接祖先的作者,出现在 Unix 的第七版贝尔实验室研究版本中。

Bash 在很大程度上与 sh 兼容,并结合了 Korn shell ksh 和 C shell csh 的有用功能。它旨在成为 IEEE POSIX 规范(IEEE 标准 1003.1)的 IEEE POSIX Shell 和工具部分的一致实现。它为交互式和编程使用提供了对 sh 的功能改进。

虽然 GNU 操作系统提供了其他 shell,包括 csh 的一个版本,但Bash 是默认的 shell。像其他 GNU 软件一样,Bash 非常便携。它目前运行在几乎所有版本的 Unix 和一些其他操作系统上——MS-DOS、OS/2 和 Windows 平台存在独立支持的端口。

回答by Andrzej Pronobis

Other answers generally pointed out the difference between Bash and a POSIX shell standard. However, when writing portable shell scripts and being used to Bash syntax, a list of typical bashisms and corresponding pure POSIX solutions is very handy. Such list has been compiled when Ubuntu switched from Bash to Dash as default system shell and can be found here: https://wiki.ubuntu.com/DashAsBinSh

其他答案通常指出了 Bash 和 POSIX shell 标准之间的区别。但是,在编写可移植的 shell 脚本并习惯于 Bash 语法时,列出典型的 bashism 和相应的纯 POSIX 解决方案非常方便。当 Ubuntu 从 Bash 切换到 Dash 作为默认系统 shell 时,已经编译了这样的列表,可以在这里找到:https: //wiki.ubuntu.com/DashAsBinSh

Moreover, there is a great tool called checkbashismsthat checks for bashisms in your script and comes handy when you want to make sure that your script is portable.

此外,还有一个很棒的工具叫做checkbashisms,它可以检查脚本中的 bashisms,当你想确保你的脚本是可移植的时,它会派上用场。

回答by Ryan Taylor

They're nearly identical but bashhas more featuresshis (more or less) an older subset of bash.

它们几乎相同,但bash具有更多功能—— sh(或多或少)是bash.

shoften means the original Bourne shell, which predates bash(Bourne *again* shell), and was created in 1977. But, in practice, it may be better to think of it as a highly-cross-compatible shell compliant with the POSIX standard from 1992.

sh通常表示1977 年创建的Bourne shell早于bash( Bourne *again* shell)的原始版本。但实际上,最好将其视为符合 1992 年 POSIX 标准的高度交叉兼容的 shell。

Scripts that start with #!/bin/shor use the shshell usually do so for backwards compatibility. Any unix/linux OS will have an shshell. On Ubuntu shoften invokes dashand on MacOS it's a special POSIX version of bash. These shells may be preferred for standard-compliant behavior, speed or backwards compatibility.

#!/bin/shshshell开头或使用shell 的脚本通常这样做是为了向后兼容。任何 unix/linux 操作系统都会有一个shshell。在 Ubuntu 上sh经常调用dash,在 MacOS 上它是bash. 这些外壳可能是符合标准的行为、速度或向后兼容性的首选。

bashis newer than the original sh, adds more features, and seeks to be backwards compatible with sh. In theory, shprograms should run in bash. bashis available on nearly all linux/unix machines and usually used by default – with the notable exception of MacOS defaulting to zshas of Catalina (10.15). FreeBSD, by default, does not come with bashinstalled.

bash比原始版本更新sh,添加了更多功能,并寻求向后兼容sh. 理论上,sh程序应该在bash. bash几乎在所有 linux/unix 机器上都可用,并且通常默认使用——MacOS 的显着例外默认zsh为 Catalina (10.15)。默认情况下,FreeBSD 不附带bash安装。

回答by Gopika BG

bash and sh are two different shells. Basically bash is sh, with more features and better syntax. Most commands work the same, but they are different.Bash (bash) is one of many available (yet the most commonly used) Unix shells. Bash stands for "Bourne Again SHell",and is a replacement/improvement of the original Bourne shell (sh).

bash 和 sh 是两个不同的 shell。bash 基本上是 sh,具有更多功能和更好的语法。大多数命令的工作方式相同,但它们不同。Bash (bash) 是许多可用(但最常用)的 Unix shell 之一。Bash 代表“Bourne Again SHell”,是对原始 Bourne shell (sh) 的替代/改进。

Shell scripting is scripting in any shell, whereas Bash scripting is scripting specifically for Bash. In practice, however, "shell script" and "bash script" are often used interchangeably, unless the shell in question is not Bash.

Shell 脚本是在任何 shell 中编写脚本,而 Bash 脚本是专门为 Bash 编写的脚本。然而,在实践中,“shell 脚本”和“bash 脚本”经常互换使用,除非所讨论的 shell 不是 Bash。

Having said that, you should realize /bin/sh on most systems will be a symbolic link and will not invoke sh. In Ubuntu /bin/sh used to link to bash, typical behavior on Linux distributions, but now has changed to linking to another shell called dash. I would use bash, as that is pretty much the standard (or at least most common, from my experience). In fact, problems arise when a bash script will use #!/bin/sh because the script-maker assumes the link is to bash when it doesn't have to be.

话虽如此,您应该意识到大多数系统上的 /bin/sh 将是一个符号链接并且不会调用 sh。在 Ubuntu 中,/bin/sh 用于链接到 bash,这是 Linux 发行版上的典型行为,但现在已更改为链接到另一个名为 dash 的 shell。我会使用 bash,因为这几乎是标准(或者至少是最常见的,根据我的经验)。事实上,当 bash 脚本将使用 #!/bin/sh 时会出现问题,因为脚本制作者假定链接是到 bash 而不是必须的。

回答by Keith Thompson

/bin/shmay or may not invoke the same program as /bin/bash.

/bin/sh可能会也可能不会调用与 相同的程序/bin/bash

shsupports at leastthe features required by POSIX(assuming a correct implementation). It may support extensions as well.

sh至少支持POSIX 所需的功能(假设实现正确)。它也可能支持扩展。

bash, the "Bourne Again Shell", implements the features required for sh plus bash-specific extensions. The full set of extensions is too long to describe here, and it varies with new releases. The differences are documented in the bash manual. Type info bashand read the "Bash Features" section (section 6 in the current version), or read the current documentation online.

bash,即“Bourne Again Shell”,实现了 sh 所需的功能以及特定于 bash 的扩展。完整的扩展集太长而无法在此描述,并且随着新版本的发布而变化。这些差异记录在 bash 手册中。键入info bash并阅读“Bash 功能”部分(当前版本中的第 6 部分),或在线阅读当前文档