The Artima Developer Community

Appendix A of Programming in Scala, First Edition
Scala scripts on Unix and Windows
by Martin Odersky, Lex Spoon, and Bill Venners
December 10, 2008

If you're on some flavor of Unix, you can run a Scala script as a shell script by prepending a "pound bang" directive at the top of the file. For example, type the following into a file named helloarg:

  #!/bin/sh
  exec scala "$0" "$@"
  !#
  // Say hello to the first argument
  println("Hello, "+ args(0) +"!")

The initial #!/bin/sh must be the very first line in the file. Once you set its execute permission:

  $ chmod +x helloarg

You can run the Scala script as a shell script by simply saying:

  $ ./helloarg globe

If you're on Windows, you can achieve the same effect by naming the file helloarg.bat and placing this at the top of your script:

  ::#!
  @echo off
  call scala %0 %*
  goto :eof
  ::!#


Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use