What is the meaning of !# (bang-pound) in a sh / Bash shell script?

bash, command-line, scala, shell

Solution

From the original documentation:

Script files may have an optional header that is ignored if present. There are two ways to format the header: either beginning with #! and ending with !#, or beginning with ::#! and ending with ::!#.

So the following code is just a header for a Scala script:

#!/usr/bin/env bash
exec scala "$0" "$@"
!#

Problem

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).

Original source

Related problems