24

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...)

Jens
  • 69,818
  • 15
  • 125
  • 179
Billy Moon
  • 57,113
  • 24
  • 136
  • 237

4 Answers4

27

/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".

Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57
favoretti
  • 29,299
  • 4
  • 48
  • 61
  • +1 As cross-platform as it gets, indeed. Sadly, POSIX doesn't specify where `env` is but I have yet to find a system where it isn't in `/usr/bin`. Anyone seen a funky OS where it is not? – Jens Aug 09 '12 at 19:02
  • is it possible to pass through several programs piped together in the shebang line? (`!#/usr/bin/env cat | nl`) – Billy Moon Aug 09 '12 at 19:04
  • No, impossible. Use a separate script if you must. – Jens Aug 09 '12 at 19:05
  • It's not possible, but I'm curious what you are after. I've never had the need to do so. :) – favoretti Aug 09 '12 at 19:05
  • I am just curious now - I was wanting to pass a script to coffeescript to compile, and then node to execute, but then I discovered I can just specify coffeescript in the shebang and it executes - happy days! – Billy Moon Aug 09 '12 at 19:06
  • 2
    I just found out that in OpenServer 5.0.6 and Unicos 9.0.2 there is only `/bin/env`... – Jens Aug 09 '12 at 19:15
  • The env looked like it was quoted from some other source. Is there a reference? – Henk Langeveld Aug 09 '12 at 20:19
  • Yes, it's a quote from `ENV(1) BSD General Commands Manual`. Do I need to specify this reference somehow special? – favoretti Aug 09 '12 at 20:20
  • 1
    adding to the "other `env`s" experience: the supercomputer I work with is `/bin/env` which makes developing for both my local machine and the supercomputer extremely annoying. – Carl Jan 22 '16 at 16:24
0

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.

sorin
  • 161,544
  • 178
  • 535
  • 806
-3

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

dadinck
  • 1,118
  • 1
  • 7
  • 8
-4

Dont use shebang.

node <<eof
your node program here
eof
hipe
  • 802
  • 6
  • 8
  • 1
    I'm not the downvoter, but this doesn't even address your question and thus can't solve your problem. – Jens Aug 09 '12 at 19:07
  • 1
    It works on all POSIX compliant systems, you can add options to the command and redirect output. On linux this #!/usr/bin/env sh -x does not work. – hipe Aug 09 '12 at 19:31
  • 3
    Just for a little background information, the shebang directs the OS to kill the new shell it spawned and re-run the entire script with the program after the shebang. Without the shebang, it uses that shell to deal with your output, and, depending on the shell, this *can*, in very specific circumstances, have unintended/bad results. Not to mention the silly extra process (not that that's something you need to worry about in practice). The downvote isn't really deserved, even if my kneejerk would have been the downvote; I was taught to use shebangs even for shell scripts. – Pyrocater Jun 09 '14 at 14:21