Shell script shebang for unknown path
Asked Answered
O

4

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

Overshoot answered 9/8, 2012 at 18:40 Comment(0)
S
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".

Selfconfessed answered 9/8, 2012 at 18:50 Comment(9)
+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?Hatfield
is it possible to pass through several programs piped together in the shebang line? (!#/usr/bin/env cat | nl)Overshoot
No, impossible. Use a separate script if you must.Hatfield
It's not possible, but I'm curious what you are after. I've never had the need to do so. :)Selfconfessed
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!Overshoot
I just found out that in OpenServer 5.0.6 and Unicos 9.0.2 there is only /bin/env...Hatfield
The env looked like it was quoted from some other source. Is there a reference?Eastbound
Yes, it's a quote from ENV(1) BSD General Commands Manual. Do I need to specify this reference somehow special?Selfconfessed
adding to the "other envs" experience: the supercomputer I work with is /bin/env which makes developing for both my local machine and the supercomputer extremely annoying.Phototopography
V
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.

Vat answered 19/3, 2017 at 20:3 Comment(0)
W
-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

Wilson answered 9/8, 2012 at 18:43 Comment(1)
not working on debian: -bash: ./foo: bash: bad interpreter: No such file or directoryCornered
P
-4

Dont use shebang.

node <<eof
your node program here
eof
Postexilian answered 9/8, 2012 at 18:49 Comment(3)
I'm not the downvoter, but this doesn't even address your question and thus can't solve your problem.Hatfield
It works on all POSIX compliant systems, you can add options to the command and redirect output. On linux this <code> #!/usr/bin/env sh -x</code> does not work.Postexilian
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.Cilia

© 2022 - 2024 — McMap. All rights reserved.