sh和bash的区别 [英] Difference between sh and bash

查看:313
本文介绍了sh和bash的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写shell程序,我们经常使用 / bin / 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.

有什么庆典 SH

什么是我们需要意识到庆典编程时和 SH

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

推荐答案

SH (或shell命令行语言)是由的 POSIX
标准

它有许多的实现( ksh88 破折号,...)。 庆典,也可以
考虑 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).

由于 SH 是一种规范,而不是实施 / bin / sh的是一个符号链接
(或硬链接)和实际执行上大多数POSIX系统。

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.

庆典开始作为一个 SH 兼容实现(尽管它predates由少数POSIX标准年),但随着时间的推移,它已经收购了很多扩展。许多这些扩展可以自行更改有效POSIX shell脚本的行为,所以庆典不是一个有效的POSIX外壳。相反,它是POSIX shell语言的一种方言。

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.

庆典支持 - POSIX 开关,这使得它更符合POSIX标准。它也试图模仿POSIX如果援引为 SH

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

在很长一段时间, / bin / sh的用来指向 /斌/庆典在大多数GNU / Linux系统。其结果是,它已经几乎成为安全地忽略这两者之间的区别。但是,开始改变最近

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.

系统其中 / bin / sh的不指向 /斌/庆典一些受欢迎的例子(和其中一些 /斌/庆典可能根本不存在)是:

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. 现代Debian和Ubuntu系统中,默认情况下符号链接 SH 破折号;

  2. Busybox的,这是通常在Linux系统启动时间的initramfs的<一部分运行/ code>。它使用灰分实施外壳

  3. BSD系统,和一般的任何非Linux系统。 OpenBSD使用 pdksh程序,在Korn shell的后裔。 FreeBSD的 SH 是原来UNIX的Bourne shell的后裔。 Solaris提供了自己的 SH 这很长一段时间不符合POSIX标准;免费实现可从传家宝项目

  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. BSDs, and in general any non-Linux systems. OpenBSD uses pdksh, a descendant of the Korn shell. 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.

归根结底,它是由你来决定,通过写«家当»线使用哪一个。

Shebang line

Ultimately, it's up to you to decide which one to use, by writing the «shebang» line.

例如

#!/bin/sh

将使用 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

要使用哪一个

有关我自己的脚本,我preFER SH ,原因如下:


  • 这是标准化

  • 这是更简单,更容易学习

  • 这是整个POSIX系统便携式 - 即使它们碰巧庆典,他们都要求有 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

有使用庆典以及优势。它的功能使编程更加方便,类似于其他现代编程语言编程。这些包括像范围的局部变量和数组。平原 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.

这篇关于sh和bash的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆