pf_miles
pf_miles

Reputation: 927

Two ways of starting a scala script, which is preferable?

I've learnt from the book Programming in Scala that I could start a scala script by writing:

#!/bin/sh
exec scala "$0" "$@"
!#
println("hello world")

That's ok, but I also tried this style:

#!/usr/bin/env scala
!#
println("hello world")

And found this one also runs correctly.
So I've no idea what's the difference between the two.
And, if both ok, why the book choose the former one to demonstrate, which looks a bit longer?

Upvotes: 8

Views: 209

Answers (1)

Fred Foo
Fred Foo

Reputation: 363627

They're equivalent. The difference is that the latter runs a shell process to start the Scala interpreter, while the former uses the env program, which is more lightweight than a shell, and obviously doesn't require mixing shell code and Scala in a single file (which might upset your editor and other tools).

Upvotes: 5

Related Questions