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
, ...). (它具有许多实现( ksh88
, dash
,...)。) 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
)的系统的一些流行示例是:)
- Modern Debian and Ubuntu systems, which symlink
sh
to dash
by default; (现代Debian和Ubuntu系统,其符号链接sh
以dash
默认;)
- 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实现。)
- 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 find和FreeBSD 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
是一种非常简约的编程语言。)