为什么#!/usr/bin/env bash 优于#!/bin/bash? [英] Why is #!/usr/bin/env bash superior to #!/bin/bash?

查看:41
本文介绍了为什么#!/usr/bin/env bash 优于#!/bin/bash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在很多地方都看到过,包括在这个网站上的推荐(首选的 Bash shebang 是什么?),优先使用 #!/usr/bin/env bash 而不是 #!/bin/bash.我什至看到一位有进取心的人 建议使用 #!/bin/bash错误,这样做会丢失 bash 功能.

I've seen in a number of places, including recommendations on this site (What is the preferred Bash shebang?), to use #!/usr/bin/env bash in preference to #!/bin/bash. I've even seen one enterprising individual suggest using #!/bin/bash was wrong and bash functionality would be lost by doing so.

综上所述,我在严格控制的测试环境中使用 bash,其中流通中的每个驱动器本质上都是单个主驱动器的克隆.我理解可移植性的论点,尽管它不一定适用于我的情况.是否有任何其他原因更喜欢 #!/usr/bin/env bash 而不是替代方案,假设可移植性是一个问题,是否有任何理由使用它会破坏功能?

All that said, I use bash in a tightly controlled test environment where every drive in circulation is essentially a clone of a single master drive. I understand the portability argument, though it is not necessarily applicable in my case. Is there any other reason to prefer #!/usr/bin/env bashover the alternatives and, assuming portability was a concern, is there any reason using it could break functionality?

推荐答案

#!/usr/bin/env 搜索 PATH for bashbash 并不总是在 /bin 中,尤其是在非 Linux 系统上.例如,在我的 OpenBSD 系统上,它位于 /usr/local/bin 中,因为它是作为可选包安装​​的.

#!/usr/bin/env searches PATH for bash, and bash is not always in /bin, particularly on non-Linux systems. For example, on my OpenBSD system, it's in /usr/local/bin, since it was installed as an optional package.

如果您绝对确定 bash/bin 中并且将始终存在,那么将它直接放在您的shebang 中没有什么坏处——但我建议您不要这样做因为脚本和程序的生命力都超出了我们最初认为的范围.

If you are absolutely sure bash is in /bin and will always be, there's no harm in putting it directly in your shebang—but I'd recommend against it because scripts and programs all have lives beyond what we initially believe they will have.

这篇关于为什么#!/usr/bin/env bash 优于#!/bin/bash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆