문제

Is it possible to specify a shebang line without knowing the path of the program you want to do the executing?

maybe don't specify the path

#!node

or specify several options

#!/usr/local/bin/node
#!/usr/bin/node

Extra points for cross platform solution (various flavors of linux, BSD, OSX etc...)

도움이 되었습니까?

해결책

/usr/bin/env is specifically thought of for cross-platform solutions.

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.

so something in lines of:

#!/usr/bin/env node

Will be cross-platform and "the right way to go".

다른 팁

Contrary to what people may think there is not standard location for env so we can only grab some info regarding it's location:

  • /usr/bin/env - MacOS (10.12)
  • both /bin/env, /usr/bin/env - Fedora (25)

I am sure others will be able to extend the list.

Put a space after the shebang. If the program is in environment variable PATH, it should go.

#! perl

Of course, a special case for Perl would be

:
eval 'exec perl -S $0 ${1+"$@"}'
  if 0;

This works on unix and OSX, even when there is no /usr/bin/env as noted by @Jens

Dont use shebang.

node <<eof
your node program here
eof
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top