7

What is the accepted, portable way to include interpreter options in the shebang line, ie. how can I do something like

#!/usr/bin/env  python -c 

or (more importantly) something like

#!/usr/bin/env java -cp "./jars/*:./src" -Xmn1G -Xms1G -server

and get it to be parsed correctly? Right now ubuntu seems to just glom the whole thing together, although other systems will parse this with no problem.

http://en.wikipedia.org/wiki/Shebang_%28Unix%29

describes the problem but offers no solution.

Robert McIntyre
  • 417
  • 3
  • 9
  • 5
    why do you need it for java? You can't put it in neither class nor jar file, and nothing else is executable with java. I suggest you describe the whole problem, there might be better solutions – unbeli Jul 15 '10 at 20:59
  • There are other languages than java that use the java virtual machine. I'm trying to do scripting in clojure, where doing something like this makes good sense. – Robert McIntyre Jul 15 '10 at 21:58
  • What do you want to do? Is it that when you do `./Main.java` it will compile and execute the java source file? If so, there is a `javac` missing of course to start with. Or do you want to paste the shebang on top of the `.class` file and do `./Main.class? If so, http://stackoverflow.com/questions/1667830/running-a-jar-file-without-directly-calling-java is the answer. See also: http://stackoverflow.com/questions/4303128/how-to-use-multiple-arguments-with-a-shebang-i-e – Ciro Santilli OurBigBook.com May 22 '15 at 19:03

1 Answers1

5

There's no good solution, as different unices treat multi-word #! lines differently. Portable #! use limits you to at most one argument to the interpreter on the #! line, and no whitespace in the interpreter or argument.

If the language allows it, you can make the script a shell script which takes care of loading the interpreter with whatever command line it likes. For example, in Perl, from the perl manual:

#!/bin/sh -- # -*- perl -*- -p
eval 'exec perl -wS "$0" ${1+"$@"}'
if $running_under_some_shell;

The shell stops processing after the second line, and Perl sees lines 2–3 as an instruction that does nothing. Some lisp/scheme dialects make #!...!# a comment, allowing you to write

#!/bin/sh
exec guile -s "$0" "$@"
!# ;; scheme code starts here

In general, the only solutions involve two files. You can write #!/usr/bin/env mywrapper where mywrapper is a program (it can be a script) that calls the actual interpreter with whatever argument it wants. Or you can make the executable itself the wrapper script and keep the interpreted file separate. The second solution has the advantage of working even if the interpreter doesn't accept a leading #! line.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • 2
    That's too bad... I really thought the shebang line would be less crippled than that given how important it is. Thanks for the advice :) – Robert McIntyre Jul 15 '10 at 23:56