Wolfram Language Scripts

Script Files

A Wolfram Language script is simply a file containing Wolfram Language commands that you would normally evaluate sequentially in a Wolfram Language session. Writing a script is useful if the commands need to be repeated many times. Collecting these commands together ensures that they are evaluated in a particular sequence with no command omitted. This is important if you run complex and long calculations. A Wolfram Language script is typically written as a file with extension .wl in the Wolfram Language source format.

When you use the Wolfram Language interactively, the commands contained in the script file can be evaluated using Get. This function can also be used programmatically in your code or other .wl files.

Get["file"]read in a file and evaluate commands in it
<<fileshorter form of Get

Reading commands from a script file.

There is no requirement concerning the structure of the script file. Any sequence of Wolfram Language commands given in the file will be read and evaluated sequentially. If your code is more complex than a plain list of commands, you may want to consider writing a more structured package, as described in "Setting Up Wolfram Language Packages".

The Wolfram Language script is more useful when there is no need for an interactive session; that is, when your script encapsulates a single calculation that needs to be performedfor example, if your calculation involves heavy computational tasks, such as linear algebra, optimization, numerical integration, or solution of differential equations, and when you do not use typesetting, dynamic interactivity, or notebooks.

Running the Script

The script file can be used when invoking the Wolfram Language kernel from the command line, using the following typical locations for the kernel executable.

$ "%ProgramFiles%\Wolfram Research\Mathematica\11.0\wolfram" -script file.wl

Running the script file on Windows.

$ /Applications/Mathematica.app/Contents/MacOS/WolframKernel -script file.wl

Running the script file on Mac OS X.

$ wolfram -script file.wl

Running the script on Linux.

The -script command line option specifies that the Wolfram Language kernel is to be run in a special script, or batch, mode. In this mode, the kernel reads the specified file and sequentially evaluates its commands. The kernel turns off the default linewrapping by setting the PageWidth option of the output functions to Infinity and prints no In[] and Out[] labels. When run in this mode, the standard input and output channels , , and are not redirected, and numbers are formatted in InputForm.

Running WolframKernel with the -script option is equivalent to reading the file using the Get command, with a single difference: after the last command in the file is evaluated, the kernel terminates. This behavior may have an effect on the Wolfram Symbolic Transfer Protocol (WSTP) connections or external processes that were created by running the script.

Unix Script Executables

Unix-like operating systems allow writing scripts that can be made executable and run as regular executable programs. This is done by putting an "interpreter" line at the beginning of the file. The same can be done with the script containing Wolfram Language commands.

The "interpreter" line consists of two characters, #!, which must be the first characters in the file, followed by the absolute path to the wolframscript interpreter, followed by other arguments.


(* generate high-precision samples of a mixed distribution *)
Print /@ RandomVariate[MixtureDistribution[
10, WorkingPrecision -> 50]

Example of a script file.

The path to the interpreter must be an absolute path because the operating system mechanism used to launch the script does not use PATH or other means to find the file. Also, the path may not contain spaces, so if you installed the Wolfram System in a location whose absolute path has spaces in it, you will need to make a symbolic link to wolframscript in an appropriate location. Giving the absolute path of the symbolic link is acceptable in the interpreter line, and it will be properly resolved.

To make the script executable, you need to set executable permissions. After that, the script can be run simply by typing its name at a shell prompt.

$ chmod a+x script.m
$ script.m

Make the script executable and run it.

The wolframscript interpreter sets up the system environment and then launches the Wolfram Language kernel.

The interpreter line may additionally contain other parameters for the interpreter. Possible parameters are specified on the wolframscript page.

#!/usr/local/bin/wolframscript -linewise -format XML

Interpreter line using additional parameters.

The Wolfram Language script does not need to have the .wl extension. An executable script is a full-featured program equivalent to any other program in a Unix operating system, so it can be used in other scripts, in pipes, subject to job control, etc. Each Wolfram Language script launches its own copy of the WolframKernel, which does not share variables or definitions. Note that running Wolfram Language scripts concurrently may be affected by the licensing restriction on how many kernels you may run simultaneously.

Executable script files can be transparently read and evaluated in an interactive Wolfram Language session. The Get command will normally ignore the first line of the script if it starts with the #! characters.

Script Parameters

When running a Wolfram Language script, you may often want to modify the behavior of the script by specifying parameters on the command line. It is possible for the Wolfram Language code to access parameters passed to the Wolfram Language script via $ScriptCommandLine. Additionally, the contents of standard input are available to be processed as a string in the variable $ScriptInputString.

$ScriptCommandLinethe command line that launched the script
$ScriptInputStringthe contents of standard input given to the script

Variables that give information about how the script is run.


(* generate "num" samples of a mixed distribution *)
num = ToExpression[$ScriptCommandLine[[2]]];
Print /@ RandomVariate[
{1, 2},
{NormalDistribution[1, 0.2],
NormalDistribution[3, 0.1]}
], num, WorkingPrecision -> 50]

Example of a script file, file.m, using a command-line parameter.

$ file.m 10

Run the script and specify the number of samples.

When accessed in the script, the $ScriptCommandLine is a list containing the name of the script as the first element and the rest of the command line arguments. $ScriptCommandLine follows the standard argv[] convention. Notice that this completely hides the path to the interpreter or any arguments passed to it on the #! line.

Due to the way the Unix-like operating systems execute scripts, the $ScriptCommandLine is set to a non-empty list only if the Wolfram Language kernel is invoked via wolframscript. If the script is intended to be run both in a batch mode and as a standalone Unix script, or in both Unix and Windows environments, the $ScriptCommandLine can be used to determine how the script is run. Then, both $ScriptCommandLine and $CommandLine should be used to access the command-line arguments.