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?
Another way to execute Scala code is to type it into a text file and save it with a name ending with “. scala”. We can then execute that code by typing “scala filename”. For instance, we can create a file named hello.
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).
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