什么是首选的Bash shebang?

本文翻译自:What is the preferred Bash shebang?

Is there any Bash shebang objectively better than the others for most uses? 在大多数情况下, Bash shebang是否客观上比其他人更好?

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

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. 我模糊地回忆起很久以前的一次听说,在最后添加一个破折号可以防止有人将命令传递给你的脚本,但是找不到任何有关它的细节。


#1楼

参考:https://stackoom.com/question/hXKA/什么是首选的Bash-shebang


#2楼

/bin/sh is usually a link to the system's default shell, which is often bash but on, eg, Debian systems is the lighter weight dash . /bin/sh通常是系统默认shell的链接,它通常是bash但是打开,例如,Debian系统是较轻的重量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. 无论哪种方式,最初的Bourne shell都是sh ,所以如果你的脚本使用了一些bash (第2代,“Bourne Again sh”)特定功能( [[ ]]测试,数组,各种含糖的东西等),那么你应该是更具体,使用后者。 This way, on systems where bash is not installed, your script won't run. 这样,在未安装bash的系统上,您的脚本将无法运行。 I understand there may be an exciting trilogy of films about this evolution...but that could be hearsay. 我知道可能有一部关于这种演变的令人兴奋的电影三部曲......但这可能是传闻。

Also note that when evoked as sh , bash to some extent behaves as POSIX standard sh (see also the GNU docs about this). 还要注意,当被唤起为shbash在某种程度上表现为POSIX标准 sh (另请参阅GNU文档 )。


#3楼

It really depends on how you write your bash scripts. 这实际上取决于你如何编写bash脚本。 If your /bin/sh is symlinked to bash, when bash is invoked as sh , some features are unavailable . 如果你的/bin/sh被符号链接到bash,当bash被调用为sh某些功能不可用

If you want bash-specific, non-POSIX features, use #!/bin/bash 如果您需要特定于bash的非POSIX功能,请使用#!/bin/bash


#4楼

You should use #!/usr/bin/env bash for portability : different *nixes put bash in different places, and using /usr/bin/env is a workaround to run the first bash found on the PATH . 你应该使用#!/usr/bin/env bash来实现可移植性 :不同的* nixes将bash放在不同的地方,使用/usr/bin/env是一种解决方法来运行在PATH上找到的第一个bash And sh is not bash . sh不是bash


#5楼

Using a shebang line to invoke the appropriate interpreter is not just for BASH. 使用shebang行来调用适当的解释器不仅仅适用于BASH。 You can use the shebang for any interpreted language on your system such as Perl, Python, PHP (CLI) and many others. 您可以将shebang用于系统上的任何解释语言,例如Perl,Python,PHP(CLI)和许多其他语言。 By the way, the shebang 顺便说一句,shebang

#!/bin/sh -

(it can also be two dashes, ie -- ) ends bash options everything after will be treated as filenames and arguments. (它也可以是两个破折号,即-- )结束bash选项后将被视为文件名和参数。

Using the env command 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 man pages for bash : 请务必查看bashman页:

man bash

and env : env

man env

Note: On Debian and Debian-based systems, like Ubuntu, sh is linked to dash not bash . 注意:在基于Debian和Debian的系统上,如Ubuntu, shdash而不是bash相关联。 As all system scripts use sh . 由于所有系统脚本都使用sh This allows bash to grow and the system to stay stable, according to Debian. 根据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. 另外,保持调用* nix就像我从不在shebang调用的脚本上使用文件扩展名一样,因为你不能像在Windows上那样省略可执行文件调用的扩展名。 The file command can identify it as a script. file命令可以将其标识为脚本。


#6楼

I recommend using: 我建议使用:

#!/bin/bash

It's not 100% portable (some systems place bash in a location other than /bin ), but the fact that a lot of existing scripts use #!/bin/bash pressures various operating systems to make /bin/bash at 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 env command is in /usr/bin (and I've used systems where it isn't). 有人建议 - 但不能保证env命令在/usr/bin (并且我使用的系统不在其中)。 Furthermore, this form will use the first instance of bash in the current users $PATH , which might not be a suitable version of the bash shell. 此外,此表单将在当前用户$PATH使用bash的第一个实例,这可能不是bash shell的合适版本。

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 answer to this question . 这个问题的 回答中 ,我已经更深入地讨论了权衡。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值