zstewart
zstewart

Reputation: 2177

Why does Scala use a reversed shebang (!#) instead of just setting interpreter to 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.

Upvotes: 10

Views: 407

Answers (3)

William Pursell
William Pursell

Reputation: 212288

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.

Upvotes: 3

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297205

Scala did not always support /usr/bin/env. No particular reason for it, just, I imagine, the person who wrote the shell scripting support was not familiar with that syntax, back in the mid 00's. The documentation followed what was supported, and I added /usr/bin/env support at some point (iirc), but never bothered changing the documentation, it would seem.

Upvotes: 0

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.

Upvotes: 1

Related Questions