本文翻译自: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上找到的第一个bashAnd 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 . 在我对这个问题的 回答中 ,我已经更深入地讨论了权衡。

什么是首选的Bash shebang?相关推荐

  1. linux 64位 可移植性,linux – #!/ bin / sh vs#!/ bin / bash,实现最大的可移植性

    shell脚本大致有四个级别的可移植性(就shebang行而言): >大多数可移植:使用#!/ bin / sh shebang并仅使用POSIX standard中指定的基本shell语法.这 ...

  2. 初学者编写bash脚本教程

    初学者编写bash脚本教程 bash shell script 定义 bash bash是命令语言解释器.广泛用于各种gun/unix系统上的默认命令解释器.全程叫做"Bourne-Agai ...

  3. bash study

    bash ---- ---- Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是一个程序,提供一个与用户对 ...

  4. Shell脚本攻略01-简介/终端打印

    概述  终端打印  玩转变量与环境变量  使用函数填加环境变量  通过shell进行数学运算  玩转文件描述符与重定向  数组和关联数组  使用别名  获取终端信息  获取.设置日期 ...

  5. 定制化centos7

    参考链接: http://www.smorgasbork.com/2012/01/04/building-a-custom-centos-7-kickstart-disc-part-4/ Buildi ...

  6. ma系列之-1-机器目录 系统结构

    内核功能:  进程管理  内存管理  文件系统  网络功能  硬件驱动  安全机制 ====================== Linux的基本原则: 1.由目的单一的小程序组成:组合小程序完成复杂 ...

  7. linux课程内容知识脉络,云计算学习路线教程大纲课件:Linux新手入门教程

    云计算学习路线教程大纲课件,Linux新手入门教程讲解: v> Shell编程之基础知识 No.1 Shell脚本的基本语法结构 第一行: "#!/usr/bin/env bash&q ...

  8. shell编程,实战高级进阶教学

    shell编程从入门到高级教程 一.shell编程初识 1.1 shell能做什么 1. 自动化批量系统初始化程序 (update,软件安装,时区设置,安全策略...) 2. 自动化批量软件部署程序 ...

  9. Linux高级专题详解--shell编程大全(shell变量,if语句,case语句,for循环,while循环,函数调用,数组,正则表达式,shell脚本三剑客--grep,sed,awk家族)

    shell编程 初始shell 程序 语言 编程 ---------------------------------- 语言 自然语言:汉语.英语 计算机语言:c语言.c++.(java php py ...

最新文章

  1. 树莓派 raspberry安全关机命令重启命令
  2. 如何在Blog中使用feedburner管理RSS订阅
  3. 回归评价指标MSE、RMSE、MAE、R-Squared
  4. python对比两张图片_用python实现对比两张图片的不同
  5. POJ 1258 Agri-Net (最小生成树)
  6. php数据库postgresql,PHP 操作 PostgreSQL数据库
  7. bmon:一个强大的网络带宽监视和调试工具
  8. 直播预告 | 企业CICD规模化落地
  9. 低代码,填补业务技术鸿沟 or 紧贴业务的开发时代?
  10. 百度地图加载空白颜色_详细解析百度收录和百度排名关系
  11. dev用不了_惊艳!小姐姐用动画图解 Git 的 10 大命令,这也太秀了吧!
  12. linux查看命令的帮助文档,Linux查看命令和帮助文件位置
  13. 运行 lighttrack 遇到错误和解决方法
  14. fastexcel读取excel追加写入sheet页_Python写入和读取excel
  15. python输入输出基本点
  16. delphi 防止程序双开 更好的 Best!
  17. 从编程角度阐述有限元最佳入门方法:以Abaqus 和OpenSees 为例
  18. 王道2023数据结构笔记
  19. 苹果开发者账号注册申请流程(2017.6.27)
  20. C语言求某天是一年里的第几天(详细)

热门文章

  1. GIT新建分支后idea如何拉取新分支到本地
  2. 文章中很多section标签。对收录有影响吗?
  3. 期货开户客户怎么销户
  4. 游戏原画师算计算机的一中,游戏原画设计师需要具备哪些基础和设计的能力呢?...
  5. 详解 JavaScript 的 IIFE 语法
  6. 【实习日记】SEO 优化和系统升级弹窗
  7. 外贸开发信退信严重,找EmailCamel解决!
  8. 【POJ1322】Chocolate (生成函数)
  9. 小学计算机输入法主题教研设计,三年级信息技术《贺卡制作》优秀教学设计范文...
  10. mysql数据库迁移到达梦数据库