Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

bash - sh和bash之间的区别(Difference between sh and bash)

When writing shell programs, we often use /bin/sh and /bin/bash . (在编写shell程序时,我们经常使用/bin/sh/bin/bash 。) I usually use bash , but I don't know what's the difference between them. (我通常使用bash ,但是我不知道它们之间有什么区别。)

What's main difference between bash and sh ? (bashsh之间的主要区别是什么?)

What do we need to be aware of when programming in bash and sh ? (在使用bashsh编程时,我们需要注意什么?)

  ask by Weiwei Yang translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What is sh (什么是嘘)

sh (or the Shell Command Language) is a programming language described by the POSIX standard . (sh (或Shell命令语言)是POSIX标准描述的一种编程语言。) It has many implementations ( ksh88 , dash , ...). (它具有许多实现( ksh88dash ,...)。) bash can also be considered an implementation of sh (see below). (bash也可以视为sh的实现(请参见下文)。)

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是规范而??不是实现,所以/bin/sh是到大多数POSIX系统上实际实现的符号链接(或硬链接)。)

What is bash (什么是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. (bash最初是一种与sh兼容的实现(尽管它比POSIX标准早了几年),但是随着时间的流逝,它获得了许多扩展。) Many of these extensions may change the behavior of valid POSIX shell scripts, so by itself bash is not a valid POSIX shell. (这些扩展中的许多扩展可能会更改有效POSIX Shell脚本的行为,因此bash本身不是有效的POSIX Shell。) Rather, it is a dialect of the POSIX shell language. (相反,它是POSIX Shell语言的方言。)

bash supports a --posix switch, which makes it more POSIX-compliant. (bash支持--posix开关,这使其更符合POSIX。) It also tries to mimic POSIX if invoked as sh . (如果以sh调用,它也会尝试模仿POSIX。)

sh = bash? (sh =重击?)

For a long time, /bin/sh used to point to /bin/bash on most GNU/Linux systems. (长期以来, /bin/sh在大多数GNU / Linux系统上都指向/bin/bash 。) 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: (/bin/sh不指向/bin/bash (在某些情况下甚至可能不存在/bin/bash )的系统的一些流行示例是:)

  1. Modern Debian and Ubuntu systems, which symlink sh to dash by default; (现代Debian和Ubuntu系统,其符号链接shdash默认;)
  2. Busybox , which is usually run during the Linux system boot time as part of initramfs . (Busybox ,通常在Linux系统启动期间作为initramfs一部分运行。) It uses the ash shell implementation. (它使用ash shell实现。)
  3. BSDs, and in general any non-Linux systems. (BSD,通常是任何非Linux系统。) OpenBSD uses pdksh , a descendant of the Korn shell. (OpenBSD使用pdksh ,它是Korn shell的后代。) FreeBSD's sh is a descendant of the original UNIX Bourne shell. (FreeBSD的sh是原始UNIX Bourne shell的后代。) Solaris has its own sh which for a long time was not POSIX-compliant; (Solaris具有自己的sh ,长期以来它不符合POSIX。) a free implementation is available from the Heirloom project . (Heirloom项目提供了免费的实现。)

How can you find out what /bin/sh points to on your system? (您如何找出/bin/sh指向系统上的内容?)

The complication is that /bin/sh could be a symbolic link or a hard link. (复杂之处在于/bin/sh可能是符号链接或硬链接。) 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. (实际上, -L标志同时覆盖了符号链接和硬链接,但是这种方法的缺点是它不可移植-samefile 不需要 find支持-samefile选项,尽管GNU findFreeBSD find都支持它。)

Shebang line (社bang线)

Ultimately, it's up to you to decide which one to use, by writing the ?shebang? line. (最终,由您决定要使用哪一个,编写“ shebang”行。)

Eg (例如)

#!/bin/sh

will use sh (and whatever that happens to point to), (将使用sh (以及发生的任何事件),)

#!/bin/bash

will use /bin/bash if it's available (and fail with an error message if it's not). (如果可用,将使用/bin/bash如果不可用,则失败并显示错误消息)。) Of course, you can also specify another implementation, eg (当然,您也可以指定其他实现,例如)

#!/bin/dash

Which one to use (使用哪一个)

For my own scripts, I prefer sh for 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 bash as well. (使用bash也有优势。) 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. (Plain sh是一种非常简约的编程语言。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...