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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With