How to execute Scala Script on Windows and Unix

You will be surprised to know that you can also create Scala script and execute it on Windows OS(as a bat script) as well as on Linux/Unix OS(as a shell script) without any compilation. Scripting with a very high level language makes things very easier for the programmer who has a little bit experience in Scripting. If you don’t know any scripting on Windows or Linux, No problem….!!! Scala script will enable you to write your scripts in programming language.

Let’s explore more details about it…..!!!!!

Scala Script on Windows

Windows user can create some of the useful .bat scripts using Scala. But before you proceed further just make sure that Scala is installed on your machine and %SCALA_HOME%/bin path is appended to your PATH variable.

Quick Scala Installation Steps on Windows

  • Download .msi installer file from Scala Downloads page
  • Install Scala by double clicking .msi installer
    • This will install Scala on C:\Program Files (x86)\scala OR C:\Program Files\scala path


  • You can download scala-2.11.7.tgz file from¬†Scala Downloads page and extract it to C:\Program Files path

After one of the above steps,

  • Go to My Comupter > Properties > Advance System Settings > Environment Variables
  • Create SCALA_HOME variable with the path where Scala is extracted
    • In our case it will be C:\Program Files\scala
  • Update/Create PATH variable and append ;%SCALA_HOME%/bin; to the value

That’s it you are done with the installation. You can check your Scala version by opening command prompt and typing below command

Now let’s create our scala script for printing simple message,


When you write any .bat scala script, just make sure that you write first 5 lines of above script first and than start your scripting.

Executing message.bat file from windows command prompt you will get “Hello, Welcome to Scala Script…..!!!!!” as an output

In above 5 lines, call scala command is responsible for executing your scala script where as %0 and %* are parameters

  • Parameter “%0” replaces itself with the script file name
  • Parameter “%*” passes other subsequent parameters to your script

Let’s understand this by an example,

Problem: Create a script file using Scala which accepts name and age as a parameter and print them in a message.



Place above script in C:/ drive and execute as below,

Quite Simple, right?????

Scala Script on Linux/Unix

Same as Windows in Linux also you have to make sure that your Scala installation and PATH variables are proper. You can refer to my post 4 Steps to Setup Scala On CentOS for the same.

Similar to .bat files you have to write below lines st the beginning of your shell scripts.

Command exec will work same as call command in Windows and here also we have two parameters "$0" and "$@"

  • Parameter “$0” replaces itself with the script file name
  • Parameter “$@” passes other subsequent parameters to your script

Let’s have the same example in shell script,

Before you execute above script make sure that you have execution rights to your shell script. Use below command for the same.

Now execute your shell script and check your output.

Congratulations…..!!!!! Now you can do scripting very well in Windows as well as Linux using your programming knowledge in Scala.


Leave a Reply

Notify of