是否可以在不知道要执行的程序路径的情况下指定 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
将是跨平台的“正道”。
与人们可能认为没有标准位置相反,env
因此我们只能获取有关其位置的一些信息:
/usr/bin/env
- macOS (10.12)/bin/env
,/usr/bin/env
-Fedora (25)我相信其他人将能够扩展该列表。
在 shebang 之后放置一个空格。如果程序在环境变量 PATH 中,它应该去。
#! perl
当然,Perl 的一个特例是
:
eval 'exec perl -S $0 ${1+"$@"}'
if 0;
这适用于 unix和OSX,即使没有 @Jens 指出的 /usr/bin/env
不要使用shebang。
node <<eof
your node program here
eof