Kokizzu
Kokizzu

Reputation: 26838

What does !# (reversed shebang) means?

From this link: http://scala.epfl.ch/documentation/getting-started.html

#!/bin/sh
exec scala "$0" "$@"
!#
object HelloWorld extends App {
  println("Hello, world!")
}
HelloWorld.main(args)

I know that $0 is for the script name, and $@ for all argument passed to the execution, but what does !# means (google bash "!#" symbols seems to show no result)?

does it mean exit from script and stdin comes from remaining lines?

Upvotes: 5

Views: 1009

Answers (2)

elm
elm

Reputation: 20415

A side comment, consider multiline script,

#!/bin/sh
        SOURCE="$LIB1/app.jar:$LIB2/app2.jar"
        exec scala -classpath $SOURCE -savecompiled "$0" "$@"
!#

Also note -savecompiled which can speed up reexecutions notably.

Upvotes: 2

ooga
ooga

Reputation: 15501

This is part of scala itself, not bash. Note what's happening: the exec command replaces the process with scala, which then reads the file given as "$0", i.e., the bash script file itself. Scala ignores the part between #! and !# and interprets the rest of the text as the scala program. They chose the "reverse shebang" as an appropriate counterpart to the shebang.

To see what I mean about exec replacing the process, try this simple script:

#!/bin/sh
exec ls
echo hello

It will not print "hello" since the process will be replaced by the ls process when exec is executed.

Reference: http://www.scala-lang.org/files/archive/nightly/docs-2.10.2/manual/html/scala.html

Upvotes: 9

Related Questions