为什么#!/ usr / bin / env bash优于#!/ bin / bash?(Why is #!/usr/bin/env bash superior to #!/bin/bash?)

编程入门 行业动态 更新时间:2024-10-11 21:30:49
为什么#!/ usr / bin / env bash优于#!/ bin / bash?(Why is #!/usr/bin/env bash superior to #!/bin/bash?)

我在许多地方见过,包括这个网站上的建议( 什么是首选的Bash shebang? ),使用#!/usr/bin/env bash优先于#!/bin/bash 。 我甚至看到一个进取的个人建议使用#!/bin/bash是错误的,并且bash功能将会丢失。

所有这些说,我在一个严格控制的测试环境中使用bash,每个驱动器都是单个主驱动器的克隆。 我了解可移植性的论点,虽然这不一定适用于我的情况。 有没有其他理由喜欢#!/usr/bin/env 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.

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搜索bash PATH , bash并不总是在/bin ,特别是在非Linux系统上。 例如,在我的OpenBSD系统上,它在/usr/local/bin ,因为它被安装为可选的包。

如果你绝对确定bash在/bin并且永远是,直接将它放在你的shebang中没有任何危害 - 但是我建议反对它,因为脚本和程序都超出了我们最初相信他们会拥有的生活。

#!/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.

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.

更多推荐

本文发布于:2023-07-17 15:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1146353.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:bin   usr   env   superior   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!