POSTS

Sha-bang for Scala Scripts on OSX

To write executable Scala scripts on OSX (and possibly relevant for other unixes) you have to add the script “sha-bang”/“she-bang”/“sh-bang” (#!) header in the script file above the script code.

My current take on the best sha bang for OSX

#!/bin/sh
exec scala -savecompiled -Dfile.encoding=UTF-8 "$0" $@
!#
println("Hello wörld")

Explanation:

-savecompiled Compile once and save the compiled file in a jar so that subsequent runs will not have to wait for the compilation. The jar file will be saved in the same directory as the script file.

-Dfile.encoding=UTF-8 By default the JVM will use macroman which breaks UTF-8 output with println, setting file.encoding will unbreak it!

"$0" This is the name of the script being run, note the quotation marks to make sure it works with scripts that are put in paths containing space

$@ Pass the rest of the arguments to the script in to scala as program arguments so that they are available in the args array.