什么时候使用ා更好!/bin/bash而不是ා!/shell脚本中的bin/sh?

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

什么时候使用ා更好!/bin/bash而不是ා!/shell脚本中的bin/sh?

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

今天的问答环节是由SuperUser提供的,SuperUser是Stack Exchange的一个分支,是一个由社区驱动的问答网站分组。

问题

超级用户读者Hendre想知道什么时候最好使用#!/bin/bash而不是#!/shell脚本中的bin/sh:

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

什么时候最好用#!/bin/bash而不是#!/shell脚本中的bin/sh?

答案

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

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.
  • 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.
  • 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.
  • 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 bashi**s but had #!/bin/sh suddenly broke.

However:

  • Even #!/bin/bash is not that correct. On different systems, bash might live in /usr/bin, /usr/pkg/bin, or /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.

有什么要补充的解释吗?在评论中发出声音。想从其他精通技术的Stack Exchange用户那里了解更多答案吗?在这里查看完整的讨论主题。

图片来源:维基百科

  • 发表于 2021-04-08 22:29
  • 阅读 ( 176 )
  • 分类:互联网

你可能感兴趣的文章

什么是shell脚本,为什么要使用它

... 既然您已经知道了shell脚本是什么以及它们的许多优点,您不想在任务中使用它们吗?您在使用它们时遇到了哪些问题?请在下面的评论中告诉我们。 ...

  • 发布于 2021-03-13 04:53
  • 阅读 ( 254 )

如何定制mac终端并使其更有用

...可以继续使用当前shell,但在需要运行特定任务集的任何时候都可以打开组。 ...

  • 发布于 2021-03-21 20:13
  • 阅读 ( 275 )

如何使用受限shell来限制linux用户可以做什么

受限的shell限制了用户帐户在Linux上可以做什么。受限用户不能更改其目录,您可以控制他们可以访问哪些命令。下面介绍如何在Linux上设置受限shell。 受限炮弹 受限shell不是另一个shell。这是标准外壳的另一种模式。Bash、Kor...

  • 发布于 2021-03-31 10:57
  • 阅读 ( 390 )

如何使用chsh在linux上更改默认shell

...用chsh命令将其设置为默认shell。我们会教你怎么做。 为什么贝壳很重要 shell位于您和操作系统之间。它在终端窗口内提供环境,允许您键入命令和运行程序。shell检查您的输入并找出您想要的内容。如果它能执行你的出价本身...

  • 发布于 2021-04-02 06:04
  • 阅读 ( 339 )

如何在linux上的bash中设置环境变量

...模拟。它包含将被模拟的硬件终端的类型。 用户:当前使用系统的用户的用户名。 PWD:当前工作目录的路径。 OLDPWD:移动到当前工作目录之前所处的目录。 LS\u颜色:LS使用的颜色代码列表突出显示不同的文件类型。 邮件:如...

  • 发布于 2021-04-02 06:44
  • 阅读 ( 225 )

如何在macos catalina上将默认shell更改为bash

有了macOS Catalina,苹果现在使用Zsh作为默认shell。我们喜欢Zsh,但是macOS中仍然包含可靠的老Bash shell,如果您愿意,可以快速切换回Bash。 Zsh只是新创建的用户帐户上的默认shell,因此在升级的Mac上的任何现有帐户在默认情况下仍...

  • 发布于 2021-04-03 01:32
  • 阅读 ( 206 )

如何在bash中使用变量

...令行参数。它们向命令提供信息,以便它知道您希望它做什么。如果希望ls在主目录上工作并显示隐藏文件,可以使用以下命令,其中tilde~和-a(all)选项是命令行参数: ls ~ -a 我们的脚本可以接受命令行参数。第一个参数引用...

  • 发布于 2021-04-03 02:34
  • 阅读 ( 202 )

如何在linux上使用chroot命令

...高系统的安全性。我们将向您展示最简单的使用方法。 什么是chroot? 如果您试图衡量命令的有用性,则必须考虑它提供的功能及其易用性。如果它太复杂了,人们无法使用,或者太冗长了,以至于他们不想尝试使用它,那么...

  • 发布于 2021-04-03 03:19
  • 阅读 ( 201 )

linux上的stdin、stdout和stderr是什么?

...准输出回显消息被发送到终端窗口: 让我们看看里面有什么捕获.txt文件。 cat capture.txt stderr消息在捕获.txt一如预期。 重定向stdout和stderr 当然,如果我们可以将stdout或stderr重定向到一个彼此独立的文件,我们应该能够同时将...

  • 发布于 2021-04-03 06:21
  • 阅读 ( 243 )

如何向linux shell脚本添加gui

...用了GTK组件。 捕获日期值 把日期打印到终端上对我们没什么用。如果要从脚本中调用此日历,则需要捕获选定的日期值,以便在脚本中使用它做一些有用的事情。我们还将稍微自定义日历。 我们将在日历中使用以下选项。它们...

  • 发布于 2021-04-03 07:01
  • 阅读 ( 298 )
eitw8265
eitw8265

0 篇文章

相关推荐