什么时候在Shell脚本中使用#!/ bin / bash代替#!/ bin / sh更好?

when-is-it-better-to-use-bin-bash-instead-of-bin-sh-in-a-shell-script-00

When you are creating a new shell script, you want to make sure it is as problem free as possible, but sometimes it can be a bit confusing to know which shebang is the best one for you to use. On that note, today’s SuperUser Q&A post has the answer to a confused reader’s question.

在创建新的shell脚本时,您要确保它尽可能没有问题,但是有时知道哪个shebang是最适合您使用的脚本可能会有些混乱。 关于这一点,今天的“超级用户问答”帖子解答了一个困惑的读者的问题。

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。

问题 (The Question)

SuperUser reader Hendre wants to know when it is better to use #!/bin/bash instead of #!/bin/sh in shell scripts:

超级用户阅读器Hendre想知道何时在shell脚本中使用#!/ bin / bash而不是#!/ bin / sh更好:

When is it more appropriate to use #!/bin/bash rather than #!/bin/sh in a shell script?

什么时候在shell脚本中使用#!/ bin / bash而不是#!/ bin / sh更合适?

When is it better to use #!/bin/bash instead of #!/bin/sh in a shell script?

什么时候在Shell脚本中使用#!/ bin / bash代替#!/ bin / sh更好?

答案 (The Answer)

SuperUser contributor grawity has the answer for us:

超级用户贡献者的感谢为我们提供了答案:

In short:

简而言之:

  • There are several shells which implement a superset of the POSIX sh specification. On different systems, /bin/sh might be a link to ash, bash, dash, ksh, zsh, etc. It will always be sh-compatible though, never csh or fish.

    有几个Shell实现POSIX sh规范的超集。 在不同的系统上, / bin / sh可能是ash,bash,dash,dash,ksh,zsh等的链接。尽管如此,它将始终与sh兼容,而不是csh或fish。

  • As long as you stick to sh features only, you can (and probably even should) use #!/bin/sh and the script should work fine, no matter which shell it is.

    只要仅坚持使用sh功能,就可以(甚至可能应该)使用#!/ bin / sh ,无论脚本位于哪个shell中,脚本都可以正常工作。

  • If you start using bash-specific features (i.e. arrays), you should specifically request bash because, even if /bin/sh already invokes bash on your system, it might not on everyone else’s system, and your script will not run there. The same applies to zsh and ksh, of course.

    如果您开始使用bash特有的功能(即数组),则应该特别请求bash,因为即使/ bin / sh已经在系统上调用bash,它也可能不在其他人的系统上运行,并且您的脚本将无法在其中运行。 当然,zsh和ksh也是如此。

  • Even if the script is for personal use only, you might notice that some operating systems change /bin/sh during upgrades. For example, on Debian it used to be bash, but was later replaced with the very minimal dash. Scripts which used bashisms but had #!/bin/sh suddenly broke.

    即使该脚本仅供个人使用,您也可能会注意到某些操作系统在升级过程中会更改/ bin / sh 。 例如,在Debian上它曾经是bash,但后来被极小的破折号所取代。 使用bashisms但具有#!/ bin / sh的脚本突然中断。

However:

然而:

  • Even #!/bin/bash is not that correct. On different systems, bash might live in /usr/bin, /usr/pkg/bin, or /usr/local/bin.

    甚至#!/ bin / bash也不是正确的。 在不同的系统上,bash可能位于/ usr / bin/ usr / pkg / bin/ usr / local / bin中

  • A more reliable option is #!/usr/bin/env bash, which uses $PATH. Although the env tool itself is not strictly guaranteed either, /usr/bin/env still works on more systems than /bin/bash does.

    一个更可靠的选项是#!/ usr / bin / env bash ,它使用$ PATH 。 尽管也没有严格保证env工具本身,但是/ usr / bin / env仍比/ bin / bash在更多的系统上工作。



Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

有什么补充说明吗? 在评论中听起来不错。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程

Image Credit: Wikipedia

图片来源:维基百科

翻译自: https://www.howtogeek.com/276607/when-is-it-better-to-use-bin-bash-instead-of-bin-sh-in-a-shell-script/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值