未知路径的shell脚本shebang

标签 shell shebang env

是否可以在不知道要执行的程序的路径的情况下指定 shebang 行?

也许不指定路径

#!node

或指定几个选项
#!/usr/local/bin/node
#!/usr/bin/node

跨平台解决方案的加分项(各种风格的 linux、BSD、OSX 等...)

最佳答案

/usr/bin/env专门用于跨平台解决方案。

env executes utility after modifying the environment as specified on
the command line.  The option name=value specifies an environmental
variable, name, with a value of value.  The option `-i' causes env
to completely ignore the environment it inherits.

If no utility is specified, env prints out the names and values of
the variables in the environment, with one name=value pair per line.

所以在以下几行:
#!/usr/bin/env node

将是跨平台的和“正确的道路”。

关于未知路径的shell脚本shebang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11889892/

相关文章:

ruby-on-rails - 有一条相对的shebang线

linux - 找不到 BASH 命令

arrays - 从句子中删除逗号分隔的单词列表

Makefile 作为带有 shebang 的可执行脚本?

python - 强大的Python3 shebang线?

docker - 在 Dockerfile 中覆盖 FROM 镜像的 ENV

linux - 如何设置启动时init的资源限制?

xcode - "Operation not permitted"在mac app中执行shell脚本时

apache - shell 脚本中的 CoffeeScript —— 由 Apache 运行(作为 CGI)

python - 一次在一台主机上运行多个命令