什么是首选的 Bash shebang?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10376206/
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 21:59:57  来源:igfitidea点击:

What is the preferred Bash shebang?

bashshebang

提问by Kurtosis

Is there any Bashshebang objectively better than the others for most uses?

Bash对于大多数用途,有没有客观上比其他人更好的shebang?

  • #!/usr/bin/env bash
  • #!/bin/bash
  • #!/bin/sh
  • #!/bin/sh -
  • etc
  • #!/usr/bin/env bash
  • #!/bin/bash
  • #!/bin/sh
  • #!/bin/sh -
  • 等等

I vaguely recall a long time ago hearing that adding a dash to the end prevents someone passing a command to your script, but can't find any details on that.

我依稀记得很久以前听说在末尾添加破折号可以防止有人将命令传递给您的脚本,但找不到任何详细信息。

回答by l0b0

You should use #!/usr/bin/env bashfor portability: different *nixes put bashin different places, and using /usr/bin/envis a workaround to run the first bashfound on the PATH. And shis not bash.

您应该使用#!/usr/bin/env bash便携性:不同的* nixes放在bash不同的地方,并使用/usr/bin/env是运行第一种解决方法bash上找到PATH。而sh不是bash

回答by delicateLatticeworkFever

/bin/shis usually a link to the system's default shell, which is often bashbut on, e.g., Debian systems is the lighter weight dash. Either way, the original Bourne shell is sh, so if your script uses some bash(2nd generation, "Bourne Again sh") specific features ([[ ]]tests, arrays, various sugary things, etc.), then you should be more specific and use the later. This way, on systems where bash is not installed, your script won't run. I understand there may be an exciting trilogy of films about this evolution...but that could be hearsay.

/bin/sh通常是指向系统默认 shell 的链接,这通常bash但是在 Debian 系统上是较轻的dash。无论哪种方式,原始 Bourne shell 都是sh,因此如果您的脚本使用某些bash(第二代,“Bourne Again sh”)特定功能([[ ]]测试、数组、各种含糖的东西等),那么您应该更具体并使用后者. 这样,在未安装 bash 的系统上,您的脚本将不会运行。我知道可能会有一部关于这种演变的令人兴奋的电影三部曲……但这可能是道听途说。

Also note that when evoked as sh, bashto some extent behaves as POSIX standardsh(see also the GNU docsabout this).

另请注意,当被调用为 时shbash在某种程度上表现为 POSIX 标准sh(另请参阅关于此的 GNU 文档)。

回答by Keith Thompson

I recommend using:

我建议使用:

#!/bin/bash

It's not 100% portable (some systems place bashin a location other than /bin), but the fact that a lot of existing scripts use #!/bin/bashpressures various operating systems to make /bin/bashat least a symlink to the main location.

它不是 100% 可移植的(某些系统放置bash在 以外的位置/bin),但许多现有脚本使用#!/bin/bash压力的事实迫使各种操作系统/bin/bash至少制作到主要位置的符号链接。

The alternative of:

替代方案:

#!/usr/bin/env bash

has been suggested -- but there's no guarantee that the envcommand is in /usr/bin(and I've used systems where it isn't). Furthermore, this form will use the first instance of bashin the current users $PATH, which might not be a suitable version of the bash shell.

已被建议 - 但不能保证该env命令在/usr/bin(并且我已经使用了它不在的系统)。此外,此表单将使用bash当前用户中的第一个实例$PATH,这可能不是 bash shell 的合适版本。

(But /usr/bin/envshould work on any reasonably modern system, either because envis in /usr/binor because the system does something to make it work. The system I referred to above was SunOS 4, which I probably haven't used in about 25 years.)

(但/usr/bin/env应该可以在任何合理的现代系统上运行,因为env它在/usr/bin或因为系统做了一些事情来使它工作。我上面提到的系统是 SunOS 4,我可能已经有大约 25 年没有使用过它了。)

If you need a script to run on a system that doesn't have /bin/bash, you can modify the script to point to the correct location (that's admittedly inconvenient).

如果您需要在没有 的系统上运行脚本/bin/bash,您可以修改脚本以指向正确的位置(这确实不方便)。

I've discussed the tradeoffs in greater depth in my answerto this question.

我在讨论更深入的权衡我的回答这个问题

A somewhat obscure update: One system I use, Termux, a desktop-Linux-like layer that runs under Android, doesn't have /bin/bash(bashis /data/data/com.termux/files/usr/bin/bash) -- but it has special handling to support #!/bin/bash.

一个有点晦涩的更新:我使用的一个系统Termux是一个在 Android 下运行的类似桌面 Linux 的层,没有/bin/bashbash/data/data/com.termux/files/usr/bin/bash)——但它有特殊的处理来支持#!/bin/bash.

回答by Jamie R Robillard Sr.

Using a shebang line to invoke the appropriate interpreter is not just for BASH. You can use the shebang for any interpreted language on your system such as Perl, Python, PHP (CLI) and many others. By the way, the shebang

使用 shebang 行调用适当的解释器不仅适用于 BASH。您可以将 shebang 用于系统上的任何解释性语言,例如 Perl、Python、PHP (CLI) 和许多其他语言。顺便说一下,shebang

#!/bin/sh -

(it can also be two dashes, i.e. --) ends bash options everything after will be treated as filenames and arguments.

(它也可以是两个破折号,即--)结束 bash 选项之后的所有内容都将被视为文件名和参数。

Using the envcommand makes your script portable and allows you to setup custom environments for your script hence portable scripts should use

使用该env命令使您的脚本可移植,并允许您为脚本设置自定义环境,因此可移植脚本应使用

#!/usr/bin/env bash

Or for whatever the language such as for Perl

或者任何语言,例如 Perl

#!/usr/bin/env perl

Be sure to look at the manpages for bash:

请务必查看以下man页面bash

man bash

and env:

env

man env

Note: On Debian and Debian-based systems, like Ubuntu, shis linked to dashnot bash. As all system scripts use sh. This allows bash to grow and the system to stay stable, according to Debian.

注意:在 Debian 和基于 Debian 的系统上,如 Ubuntu,sh链接到dashnot bash。由于所有系统脚本都使用sh. 根据 Debian 的说法,这允许 bash 增长并使系统保持稳定。

Also, to keep invocation *nix like I never use file extensions on shebang invoked scripts, as you cannot omit the extension on invocation on executables as you can on Windows. The file command can identify it as a script.

此外,为了保持调用 *nix 就像我从来没有在 shebang 调用的脚本上使用文件扩展名一样,因为您不能像在 Windows 上那样省略可执行文件的调用扩展名。file 命令可以将其识别为脚本。

回答by glenn Hymanman

It really depends on how you write your bash scripts. If your /bin/shis symlinked to bash, when bash is invoked as sh, some features are unavailable.

这实际上取决于您如何编写 bash 脚本。如果您/bin/sh已符号链接到 bash,则当 bash 被调用为 时sh某些功能将不可用

If you want bash-specific, non-POSIX features, use #!/bin/bash

如果您想要特定于 bash 的非 POSIX 功能,请使用 #!/bin/bash