I want to understand how this Scala script works:
#!/usr/bin/env bash
exec scala "$0" "$@"
!#
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world! " + args.toList)
}
}
HelloWorld main args
On line 3, what is "!#" doing? Is the remainder of the file then fed to standard input of the Scala program? Also, is '!#' documented anywhere?
NB: The nearest thing I could find, although it is not directly relevant in any way is Stack Overflow question Why do you need to put #!/bin/bash at the beginning of a script file? (about the beginning of a Bash script).
When I removed
!#
in the sample program in BluesRockAddict's answer, I get the following error at the console:From the above error message, I understood the following things:
!#
is the close tag for the headerexec scala "$0" "$@"
, which probably tells Scala that whatever comes after!#
is the Scala code to be executed.!#
can be replaced with::!#
.From the original documentation:
So the following code is just a header for a Scala script:
Based purely on experimentation (I assume this is described somewhere):
Beginning with Scala 2.10.0, the non-standard (from a POSIX scripting POV) pound-bang (!#) is no longer required in a POSIX shell environment. The following single line works (but must be the first line of the script, with no leading spaces):
However, the only way to provide a shebang in a windows batch file is to trick it into calling Scala with itself as the first argument. Consequently, Scala needs to know where the last line of the batch file is, and therefore requires the closing ::!# line. Here's a working example:
Correction: I originally had #!/usr/bin/env scalav (notice the trailing v), which is my script to define property "scala.script", add some libraries to the classpath, before 'exec scala' itself. That way it's possible to determine the name of the script that is being executed, although scalav must then be in your PATH.