“#!/usr/bin/env bash"和“#!/usr/bin/env bash"有什么区别?和“#!/usr/bin/bash"? [英] What is the difference between "#!/usr/bin/env bash" and "#!/usr/bin/bash"?

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

问题描述

在 Bash 脚本的标题中,这两个语句之间的区别是什么:

In the header of a Bash script, what's the difference between those two statements:

  1. #!/usr/bin/env bash

#!/usr/bin/bash

当我查阅 env 手册页时,我得到这个定义:

When I consulted the env man page, I get this definition:

 env - run a program in a modified environment

什么意思?

推荐答案

通过 /usr/bin/env 运行命令的好处是可以查找程序的默认版本当前环境熨烫.

Running a command through /usr/bin/env has the benefit of looking for whatever the default version of the program is in your current environment.

这样,您就不必在系统上的特定位置查找它,因为这些路径可能位于不同系统上的不同位置.只要它在你的路径中,它就会找到它.

This way, you don't have to look for it in a specific place on the system, as those paths may be in different locations on different systems. As long as it's in your path, it will find it.

一个缺点是,如果您希望支持 Linux,您将无法传递多个参数(例如,您将无法编写 /usr/bin/env awk -f),如POSIX 对如何解释该行含糊不清,Linux 将第一个空格之后的所有内容解释为表示单个争论.你可以在某些版本的 env 上使用 /usr/bin/env -S 来解决这个问题,但是脚本会变得更不便携并且在相当新的系统上会崩溃(例如,甚至 Ubuntu 16.04,如果不是更高的话).

One downside is that you will be unable to pass more than one argument (e.g. you will be unable to write /usr/bin/env awk -f) if you wish to support Linux, as POSIX is vague on how the line is to be interpreted, and Linux interprets everything after the first space to denote a single argument. You can use /usr/bin/env -S on some versions of env to get around this, but then the script will become even less portable and break on fairly recent systems (e.g. even Ubuntu 16.04 if not later).

另一个缺点是,由于您没有调用显式可执行文件,因此有可能出错,并且在多用户系统上存在安全问题(如果有人设法在您的路径中获得名为 bash 的可执行文件,例如).

Another downside is that since you aren't calling an explicit executable, it's got the potential for mistakes, and on multiuser systems security problems (if someone managed to get their executable called bash in your path, for example).

#!/usr/bin/env bash #lends you some flexibility on different systems
#!/usr/bin/bash     #gives you explicit control on a given system of what executable is called

在某些情况下,第一个可能是首选(例如使用多个版本的 python 运行 python 脚本,而不必返工可执行行).但在安全性为重点的情况下,后者将是首选,因为它限制了代码注入的可能性.

In some situations, the first may be preferred (like running python scripts with multiple versions of python, without having to rework the executable line). But in situations where security is the focus, the latter would be preferred, as it limits code injection possibilities.

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

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