“#!/ usr / bin / env bash”和“#!/ usr / bin / bash”之间有什么区别?

塔拉萨拉:

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

  1. #!/usr/bin/env bash

  2. #!/usr/bin/bash

当查阅env 手册页时,得到以下定义:

 env - run a program in a modified environment

这是什么意思?

亚历克·贝内特(Alec Bennett):

运行命令,通过/usr/bin/env具有寻找不管程序的默认版本是在当前的利益ENV ironment。

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

缺点是,/usr/bin/env awk -f如果您希望支持Linux,那么您将不能传递多个参数(例如,您将无法编写),因为POSIX对行的解释方式含糊不清,而Linux会在第一个命令之后解释所有内容表示单个参数的空格。您可以/usr/bin/env -S在的某些版本上使用env来解决此问题,但是该脚本的可移植性甚至会降低,并在相当新的系统上崩溃(例如,即使不晚于Ubuntu 16.04)。

另一个缺点是,由于您没有调用显式的可执行文件,因此很可能会出错,并且会导致多用户系统安全问题(例如,如果有人设法bash在您的路径中调用其可执行文件)。

#!/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脚本,而不必重新编写可执行代码行)。但是在以安全性为重点的情况下,后者是首选,因为它限制了代码注入的可能性。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从/ usr / bin / env或/ bin启动bash

为什么#!/ usr / bin / env bash优于#!/ bin / bash?

$BASH_VERSION vs /usr/bin/env

$BASH_VERSION 与 /usr/bin/env

/ bin和/ usr / bin /有什么区别?

/ bin和/ usr / bin之间的区别

/ usr / bin和/ usr / local / bin之间的区别

在Java中执行/ usr / bin / env bash -c“ command”

/bin/ls 和 /usr/bin/ls 的区别

time和/ usr / bin / time有什么区别?

POSIX shell脚本shebang#!/ bin / sh与#!/ usr / bin / env sh,有什么区别?

/ usr / bin和/ usr / local / bin之间的冲突

symlink / usr / bin / env损坏?

PATH 在 /usr/bin/env -i /bin/bash --norc --noprofile 之后不为空

为什么“ / usr / bin / env bash -x”仅在命令行中有效?

什么是/ usr / bin / parity?

什么是/ usr / local / bin?

python shebang与/ usr / bin / env而不是hard-path有什么区别?

为什么“#!/ usr / bin / env python”比“#!/ usr / bin / python”更正确?

BASH脚本在/ usr / bin中查找

bash:/ usr / bin / lein:权限被拒绝

终端开始给错误bash:/ usr / bin / lesspipe

VS Code 找不到 /usr/bin/bash

为什么#!/ usr / bin / env bash在我的系统上不起作用?

/ usr / bin / env和Capistrano 3在Rails部署中失败

gradlew错误:/ usr / bin / env:bash:没有这样的文件或目录

/ usr / bin / env:'bash \ r':没有这样的文件或目录

/ bin,/ sbin,/ usr / bin,/ usr / sbin,/ usr / local / bin,/ usr / local / sbin之间的差异

别名:.bash_rc,.bash_aliases和/ usr / local / bin之间的区别