sh 和 Bash 之间的区别

发布于 2024-11-02 16:42:34 字数 177 浏览 5 评论 0原文

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

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

在Bash和sh中编程时需要注意什么?

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

What's main difference between Bash and sh?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

妞丶爷亲个 2024-11-09 16:42:34

sh是什么?

sh(或 Shell 命令语言)是由 POSIX 标准。它有许多实现(ksh88Dash ,...)。 Bash 也可以被认为是 sh 的实现(见下文)。

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

什么是巴什?

Bash 最初是一个与 sh 兼容的实现(尽管它早于 POSIX 标准几年),但随着时间的推移,它已经获得了许多扩展。其中许多扩展可能会更改有效 POSIX shell 脚本的行为,因此 Bash 本身并不是有效的 POSIX shell。相反,它是 POSIX shell 语言的一种方言。

Bash 支持 --posix 开关,这使其更符合 POSIX 标准。如果作为 sh 调用,它还会尝试模仿 POSIX。

sh = bash?

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

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

  1. 现代 Debian 和 Ubuntu 系统,默认情况下将 sh 符号链接到 dash
  2. Busybox,通常在 Linux 系统启动时作为 initramfs.它使用 ash shell 实现。
  3. BSD 系统,以及一般的任何非 Linux 系统。 OpenBSD 使用 pdksh,它是 KornShell。 FreeBSD 的 sh 是原始 Unix Bourne shell 的后代。 Solaris 有自己的 sh,长期以来不符合 POSIX 标准; Heirloom 项目 提供免费实现。

如何找出 /bin/sh 在您的系统上指向什么?

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

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

:这是一个硬链接,请尝试

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

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

Shebang 行

最终,您可以通过将“shebang”行写为脚本的第一行来决定使用哪一个。

例如,

#!/bin/sh

将使用 sh (以及任何它所指向的内容),

#!/bin/bash

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

#!/bin/dash

使用哪个实现

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

  • 它是标准化的
  • 它更简单且更容易学习
  • 它是可移植的POSIX 系统 — 即使它们碰巧没有 bash,它们也需要有 sh

使用 bash 也有优点。它的特性使编程更加方便并且类似于其他现代编程语言的编程。其中包括作用域局部变量和数组等。 Plain sh 是一种非常简约的编程语言。

What is sh?

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

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

What is Bash?

Bash started 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 Bash is not a valid POSIX shell. Rather, it is a dialect of the POSIX shell language.

Bash supports a --posix switch, which makes it more POSIX-compliant. It also tries to mimic POSIX if invoked as sh.

sh = bash?

For a long time, /bin/sh used to point to /bin/bash on 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.

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

  1. Modern Debian and Ubuntu systems, which symlink sh to dash by default;
  2. Busybox, which is usually run during the Linux system boot time as part of initramfs. It uses the ash shell implementation.
  3. BSD systems, and in general any non-Linux systems. OpenBSD uses pdksh, a descendant of the KornShell. FreeBSD's sh is a descendant of the original Unix Bourne shell. Solaris has its own sh which for a long time was not POSIX-compliant; a free implementation is available from the Heirloom project.

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

The complication is that /bin/sh could be a symbolic link or a hard link. If it's a symbolic link, a portable way to resolve it is:

% 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 -L flag covers both symlinks and hardlinks,
but the disadvantage of this method is that it is not portable —
POSIX does not require find to support the -samefile option, although both GNU find and FreeBSD find support it.

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.

E.g.

#!/bin/sh

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

#!/bin/bash

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

#!/bin/dash

Which one to use

For my own scripts, I prefer sh for the following reasons:

  • 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

There are advantages to using bash as 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 sh is a very minimalistic programming language.

薄荷港 2024-11-09 16:42:34

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

TL;DR:Bash 是一个sh 的超集,具有更优雅的语法和更多功能。几乎在所有情况下使用 Bash shebang 行都是安全的,因为它在现代平台。

注意:在某些环境中,sh Bash。检查sh --version

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

TL;DR: Bash is a superset of sh with 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.

NB: in some environments, sh is Bash. Check sh --version.

柠檬色的秋千 2024-11-09 16:42:34

对于尝试使用 sh 并惊讶于它的行为与 bash 不同的人来说,这个问题经常被提名为规范问题。以下是常见误解和陷阱的简要概述。

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

  • 如果您使用 sh scriptname 运行脚本,或者使用 scriptname 运行脚本并在 shebang 行,您应该期望 POSIX sh 行为。
  • 如果您使用 bash scriptname 运行脚本,或者使用 scriptname 运行脚本并在其中包含 #!/bin/bash (或本地等效项)在 shebang 行中,您应该预料到 Bash 行为。

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

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

Bash 的目标是向后兼容 Bourne shell 和 POSIX,但还有许多附加功能。 Bash 参考手册有一个 部分它试图列举差异,但一些常见的混淆来源包括

  • [[sh 中不可用(只有 [ 这是更笨重和有限)。另请参阅 Bash 中的单方括号和双方括号有什么区别?
  • sh 没有数组。
  • 一些 Bash 关键字,例如 localsourcefunctionshoptlet、< code>declare、pushdpopdselect 无法移植到 sh。 (一些 sh 实现支持例如 local。)
  • Bash 有许多 C 风格的语法扩展,例如三参数 for((i=0;i<=3 ;i++)) 循环、+= 增量赋值等。 $'string\nwith\tC\aescapes' 功能暂定为 接受 POSIX (意味着它现在可以在 Bash 中使用,但尚不被 sh 支持code> 仅遵守当前 POSIX 规范的系统,并且可能在未来一段时间内不会)。
  • Bash 支持 <<<'here strings'
  • Bash 有 *.{png,jpg}{0..12} 大括号扩展。
  • Bash 扩展了通配工具,例如用于递归子目录的 ** (globstar) 和用于使用不同的 更通用的通配符语法。
  • ~ 指的是$HOME 仅在 Bash 中(更一般的是 ~usernameusername 的主目录)。这是在 POSIX 中,但是某些 POSIX 之前的 /bin/sh 实现中可能会丢失。
  • Bash 使用 <(cmd)>(cmd) 进行进程替换。
  • Bash 支持带有 <> 重定向的协进程。
  • Bash 有 Csh 风格的方便重定向别名,例如 &| 代表 2>&1 |&> 代表 > ; ... 2>&1
  • Bash 具有一组丰富的附加非标准 参数扩展,例如 ${substring:1:2}${variable/pattern/replacement}、case Bash 扩展了 shell算术
  • 功能(尽管仍然不支持浮点)。有一个过时的遗留 $[expression] 语法,但应使用 POSIX 算术 $((expression)) 语法替换。 (不过,一些旧的 POSIX 之前的 sh 实现可能不支持这一点。)
  • 一些内置命令具有不可移植的选项,例如 type -aprintf -v 、 cd -P 和常年使用的 echo -e 。
  • 魔术变量,如 $RANDOM$_$SECONDS$PIPESTATUS[@]$ FUNCNAME 是 Bash 扩展。请参阅参考手册。
  • Bash 将一些系统工具公开为文件句柄,例如 /dev/stdin/dev/fd//dev/tcp/等语法差异,
  • 例如 export variable=valueexport variable 应与变量赋值分开)和 [ "x" == "y" ] 不可移植([ ... ] 中的可移植字符串比较使用单个等号)。
  • 许多仅 Bash 的扩展,用于启用或禁用可选行为并公开 shell 的内部状态。
  • 许多用于交互式使用的便利功能,但不会影响脚本行为。

请记住,这是一个简短的列表。请参阅参考手册了解完整内容,并http://mywiki.wooledge.org/Bashism 了解更多信息许多好的解决方法;和/或尝试 http://shellcheck.net/ 它会针对许多仅限 Bash 的功能发出警告。

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

不幸的是,当您尝试使用这些构造时,当 Bash 作为 sh 调用时,它不会发出警告。它也不会完全禁用所有仅限 Bash 的功能,因此通过将 Bash 调用为 sh 来运行 Bash 并不是检查脚本是否可以正确移植到的好方法。 ash/dash/POSIX sh 或类似 传家宝sh
如果您想检查严格的 POSIX 合规性,请尝试 posh
在其指定的 POSIX 模式下
(然而,这似乎没有正确记录)。

顺便说一句, POSIX 标准化工作旨在指定各种 U*x- 的行为类似平台行为,包括 shell (sh)。
然而,这是一个不断发展的文档,因此,某些实现遵循 POSIX 规范的早期版本;此外,还有一些遗留实现甚至没有尝试遵守 POSIX。
最初的 Bourne shell 有一些怪癖,后来由 POSIX 规范纠正,该规范很大程度上基于 ksh88。 (许多 Bash 扩展也是 ksh 的创新。)

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

First off, you should understand what to expect.

  • If you run your script with sh scriptname, or run it with scriptname and have #!/bin/sh in the shebang line, you should expect POSIX sh behavior.
  • If you run your script with bash scriptname, or run it with scriptname and have #!/bin/bash (or the local equivalent) in the shebang line, you should expect Bash behavior.

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).

So, how do they actually differ?

Bash aims to be backwards-compatible with the Bourne shell and POSIX, but has many additional features. The Bash Reference manual has a section which attempts to enumerate the differences but some common sources of confusion include

  • [[ is not available in sh (only [ which is more clunky and limited). See also What is the difference between single and double square brackets in Bash?
  • sh does not have arrays.
  • Some Bash keywords like local, source, function, shopt, let, declare, pushd, popd, and select are not portable to sh. (Some sh implementations 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 sh on 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.
  • Bash has extended globbing facilities like ** (globstar) for recursing subdirectories, and extglob for using a different, more versatile wildcard syntax.
  • ~ refers to $HOME only in Bash (and more generally ~username to the home directory of username).This is in POSIX, but may be missing from some pre-POSIX /bin/sh implementations.
  • Bash has process substitution with <(cmd) and >(cmd).
  • Bash supports coprocesses with <> redirection.
  • Bash has Csh-style convenience redirection aliases like &| for 2>&1 | and &> for > ... 2>&1
  • Bash features a rich set of additional non-standard parameter expansions such as ${substring:1:2}, ${variable/pattern/replacement}, case conversion, etc.
  • Bash has 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 sh implementations may not support that, though.)
  • Several built-in commands have options which are not portable, like type -a, printf -v, cd -P, and the perennial echo -e.
  • Magic variables like $RANDOM, $_, $SECONDS, $PIPESTATUS[@] and $FUNCNAME are Bash extensions. See the Reference Manual.
  • Bash exposes some system facilities as file handles, like /dev/stdin, /dev/fd/<number>, /dev/tcp/<network address>, etc
  • Syntactic differences like export variable=value (export variable should be separate from variable assignment) and [ "x" == "y" ] which are not portable (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.

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

A common error is to have a #!/bin/bash shebang line, but then nevertheless using sh scriptname to 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.)

Unfortunately, Bash will not warn when you try to use these constructs when it is invoked as sh. It doesn't completely disable all Bash-only functionality, either, so running Bash by invoking it as sh is not a good way to check if your script is properly portable to ash/dash/POSIX sh or variants like Heirloom sh.
If you want to check for strict POSIX compliance, try posh
in its designated POSIX mode
(which however does not seem to be properly documented).

As an aside, the POSIX standardization effort is intended to specify the behavior of various U*x-like platform behaviors, including the shell (sh).
However, this is an evolving document, and so, some implementations adhere to an earlier version of the POSIX specification; furthermore, there are some legacy implementations which didn't even try to adhere to POSIX.
The original Bourne shell had some quirks which were later straightened out by the POSIX spec, which in large parts is based on ksh88. (Many of the Bash extensions are also innovations from ksh.)

北陌 2024-11-09 16:42:34

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

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

Bash (Bourne again shell) 是 Bourne shell 的替代品。 Bash 是 sh 的超集。 Bash 支持 sh。 POSIX 是一组定义 POSIX 兼容系统应如何工作的标准。 Bash 实际上并不是一个 POSIX 兼容的 shell。在脚本语言中,我们将解释器表示为#!/bin/bash

类比:

  • Shell 就像一个接口或规范或 API。
  • sh 是一个实现 Shell 接口的类。
  • Bash 是 sh 的子类。

输入图像描述这里

Shell is 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).

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).

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.

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.

enter image description here

深空失忆 2024-11-09 16:42:34

帖子来自 UNIX.COM

Shell 功能

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

代码:

                                     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

上表的关键。

Y 功能可以使用此 shell 来完成

N 功能不存在于 shell 中

F 功能只能通过使用 shell 函数来完成 。
机制。

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

上表注释

  1. 原始版本中没有此功能,但此后已成为
    几乎标准。
  2. 此功能相当新,因此在许多设备上通常找不到
    shell 版本,它正在逐渐进入
    标准分布。
  3. 许多人认为这个 shell 的 Vi 仿真是
    不完整。
  4. 此功能不是标准功能,但存在非官方补丁
    执行此操作。
  5. 名为“pdksh”的版本是免费提供的,但没有
    AT&T 版本的全部功能。
  6. 这可以通过 shell 可编程完成机制来完成。
  7. 只能通过 ENV 环境变量指定文件。

Post from 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.

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.

N Feature is not present in the shell.

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

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

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.
胡渣熟男 2024-11-09 16:42:34

TERMINAL

  • 程序,用于打开
  • xterm、rxvt、konsole、kvt、gnome-terminal、nxterm 和 eterm 窗口。

SHELL

  • 是在终端中运行的程序
  • Shell 既是命令解释器,又是编程语言
  • Shell 简单来说就是执行命令的宏处理器。
  • 宏处理器意味着扩展文本和符号以创建更大表达式的功能。

SH 与。 BASH

SH

  • (SHell)
  • 是一种特定的 shell、
  • 命令解释器和编程语言
  • BASH 的前身

BASH

  • (Bourne-Again SHell)
  • 是一种特定的 shell、
  • 命令解释器和编程语言
  • 具有 sh 功能及更多
  • 功能 SH
  • BASH 的后继者是默认的 SHELL

参考材料:

SHELL
gnu.org:

从本质上讲,shell 只是一个执行以下操作的宏处理器
命令。术语“宏处理器”意味着文本和
符号被扩展以创建更大的表达式。

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

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

shell 允许同步执行 GNU 命令
异步地。 shell 等待同步命令完成
在接受更多输入之前;异步命令继续执行
与 shell 并行读取和执行额外的操作
命令。重定向结构允许细粒度的控制
这些命令的输入和输出。此外,外壳允许
控制命令环境的内容。

Shell 还提供一小组内置命令(builtins)
实现不可能或不方便获得的功能
单独的实用程序。 例如,cd、break、 continue 和 exec 不能
在 shell 之外实现,因为它们直接操作
外壳本身。历史记录、getopts、kill 或 pwd 内置函数,其中
其他的,可以在单独的实用程序中实现,但它们更多
作为内置命令使用起来很方便。所有 shell 内置函数都是
在后续部分中进行描述。

虽然执行命令是必不可少的,大部分的权力(和
shell 的复杂性是由于其嵌入式编程语言所致。

与任何高级语言一样,shell 提供变量、流程
控制结构、引用和函数。

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

BASH gnu.org:

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

Bash 与 sh 很大程度上兼容并包含有用的功能
来自 Korn shell ksh 和 C shell csh。它的目的是成为一个
IEEE POSIX Shell 和工具部分的一致实现
IEEE POSIX 规范(IEEE 标准 1003.1)。它提供
交互和编程方面相对于 sh 的功能改进
使用。

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

TERMINAL

  • program(s) that put a window up
  • xterm, rxvt, konsole, kvt, gnome-terminal, nxterm, and 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.

SH Vs. BASH

SH

  • (SHell)
  • Is a specific shell
  • a command interpreter and a programming language
  • Predecessor of 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

REFERENCE MATERIAL:

SHELL
gnu.org:

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, cd, break, continue, and exec cannot
be implemented outside of the shell
because 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.

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.

眼泪都笑了 2024-11-09 16:42:34

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

此外,还有一个很棒的工具,名为 checkbashisms 用于检查脚本中的 bashism,当您想确保脚本可移植时会很方便。

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

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

梦开始←不甜 2024-11-09 16:42:34

它们几乎相同,但bash具有更多功能 - sh(或多或少)是bash的较旧子集>。

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

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

bash 比原来的 sh 更新,添加了更多功能,并力求向后兼容 shsh 程序通常在 bash 中运行得很好。 bash 几乎在所有 Linux/unix 机器上都可用,并且通常默认使用 - 值得注意的例外是,从 Catalina (10.15) 开始,MacOS 默认使用 zsh。默认情况下,FreeBSD 没有安装 bash

They're nearly identical but bash has more featuressh is (more or less) an older subset of bash.

sh often 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.

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

bash is newer than the original sh, adds more features, and seeks to be backwards compatible with sh. sh programs will usually run just fine in bash. bash is available on nearly all linux/unix machines and usually used by default – with the notable exception of MacOS defaulting to zsh as of Catalina (10.15). FreeBSD, by default, does not come with bash installed.

落日海湾 2024-11-09 16:42:34

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

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

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

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

sh supports at least the features required by POSIX (assuming a correct implementation). It may support extensions as well.

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 bash and read the "Bash Features" section (section 6 in the current version), or read the current documentation online.

吝吻 2024-11-09 16:42:34

以最简单的方式解释差异:

有了基本的了解后,其他答案就会更容易理解。

Shell - “Shell”是一个程序,它促进用户和操作系统(内核)之间的交互。有许多可用的 shell 实现,例如 sh、BashC shell, Z shell等。

使用任何一个shell程序,我们将能够执行该shell程序支持的命令。

Bash - 它源自Bourne-again Shell。使用这个程序,我们将能够执行 shell 指定的所有命令。此外,我们将能够执行一些专门添加到该程序中的命令。 Bash 向后兼容 sh。

Sh - 它源自 Bourne Shell。 “sh”支持 shell 中指定的所有命令。这意味着,使用这个程序,我们将能够执行Shell指定的所有命令。

有关详细信息,请参阅:

The differences explained in the easiest way possible:

After having a basic understanding, the other answers will be easier to understand.

Shell - "Shell" is a program, which facilitates the interaction between the user and the operating system (kernel). There are many shell implementations available, like sh, Bash, C shell, Z shell, etc.

Using any of the shell programs, we will be able to execute commands that are supported by that shell program.

Bash - It derived from Bourne-again Shell. Using this program, we will be able to execute all the commands specified by the shell. Also, we will be able to execute some commands that are specifically added to this program. Bash has backward compatibility with sh.

Sh - It derived from Bourne Shell. "sh" supports all the commands specified in the shell. It means, using this program, we will be able to execute all the commands specified by Shell.

For more information, see:

别靠近我心 2024-11-09 16:42:34

Linux 操作系统提供不同类型的 shell。尽管 shell 有许多共同的命令,但每种类型都有独特的功能。
让我们研究一下最常用的不同类型的 shell。

Sh shell:

Sh shell 也称为 Bourne shell。 Sh shell 是 AT&T 贝尔实验室的 Stephen Bourne 于 1977 年为 Unix 计算机开发的第一个 shell。它包括许多脚本工具。

Bash shell

Bash shell 代表 Bourne Again Shell。 Bash shell 是大多数 Linux 发行版中的默认 shell,并替代 Sh shell(Sh shell 也将在 Bash shell 中运行)。 Bash shell 无需修改即可执行绝大多数 Sh shell 脚本,并且还提供命令行编辑功能。

The Linux operating system offers different types of shell. Though shells have many commands in common, each type has unique features.
Let’s study different kind of mostly used shells.

Sh shell:

Sh shell is also known as Bourne shell. Sh shell is the first shell developed for Unix computers by Stephen Bourne at AT&T's Bell Labs in 1977. It includes many scripting tools.

Bash shell:

Bash shell stands for Bourne Again Shell. Bash shell is the default shell in most Linux distributions and substitute for the Sh shell (the Sh shell will also run in the Bash shell). The Bash shell can execute the vast majority of Sh shell scripts without modification and provide commands line editing feature also.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文