Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Scala use a reversed shebang (!#) instead of just setting interpreter to scala

Tags:

bash

sh

scala

The scala documentation shows that the way to create a scala script is like this:

#!/bin/sh
exec scala "$0" "$@"
!#
/* Script here */

I know that this executes scala with the name of the script file and the arguments passed to it, and that the scala command apparently knows to read a file that starts like this and ignore everything up to the reversed shebang !#

My question is: is there any reason why I should use this (rather verbose) format for a scala script, rather than just:

#!/bin/env scala
/* Script here */

This, as far a I can tell from a quick test, does exactly the same thing, but is less verbose.

like image 927
zstewart Avatar asked May 01 '15 21:05

zstewart


2 Answers

How old is the documentation? Usually, this sort of thing (often referred to as 'the exec hack') was recommended before /bin/env was common, and this was the best way to get the functionality. Note that /usr/bin/env is more common than /bin/env, and ought to be used instead.

like image 52
William Pursell Avatar answered Oct 13 '22 18:10

William Pursell


Note that it's /usr/bin/env, not /bin/env.

There are no benefits to using an intermediate shell instead of /usr/bin/env, except running in some rare antique Unix variants where env isn't in /usr/bin. Well, technically SCO still exists, but does Scala even run there?

However the advantage of the shell variant is that it gives an opportunity to tune what is executed, for example to add elements to PATH or CLASSPATH, or to add options such as -savecompiled to the interpreter (as shown in the manual). This may be why the documentation suggests the shell form.

I am not on the Scala development team and I don't know what the historical motivation for the Scala documentation was.

like image 42
Gilles 'SO- stop being evil' Avatar answered Oct 13 '22 17:10

Gilles 'SO- stop being evil'