*Mathematica* Scripts

### Script Files

A

*Mathematica* script is simply a file containing

*Mathematica* commands that you would normally evaluate sequentially in a

*Mathematica* 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

*Mathematica* script is typically written as a file with extension .m in the Mathematica

source format.
When you use

*Mathematica* 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 .m files.

Get["file"] | read in a file and evaluate commands in it |

<<file | shorter form of Get |

Reading commands from a script file.

There is no requirement on the structure of the script file. Any sequence of

*Mathematica* 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 *Mathematica* Packages".

*Mathematica* 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 performed—for 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

*Mathematica* kernel from the command line, assuming that the MathKernel or math executables are on your path and can be found.

$ MathKernel -script file.m

Running the script file on Mac OS X.

Running the script on Linux.

The

-script command line option specifies that the

*Mathematica* 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 line wrapping 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 the output is done in

InputForm. Thus the output of the script can be saved into a file and then subsequently read back into

*Mathematica*. The output is also suitable for passing to other scripts, so that the

MathKernel process evaluating script commands can be used in a pipe with other processes.

Running

MathKernel 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

*MathLink* 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

*Mathematica* commands.

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

MathematicaScript interpreter, followed by other arguments. The last argument on the interpreter line must be

-script. The

MathematicaScript interpreter is included in your copy of

*Mathematica*.

#!/usr/local/bin/MathematicaScript -script

(* generate high-precision samples of a mixed distribution *)

Print /@ RandomVariate[MixtureDistribution[

{1,2},

{NormalDistribution[1,2/10],

NormalDistribution[3,1/10]}],

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

*Mathematica* in a location whose absolute path has spaces in it, you will need to make a symbolic link to

MathematicaScript 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

MathematicaScript interpreter sets up the system environment and then launches the

*Mathematica* kernel. Running the

*Mathematica* script is completely equivalent to running

MathKernel -script scriptname.m.

The interpreter line may additionally contain other parameters placed between the interpreter path and the

-script option. These parameters will be passed to the

MathKernel executable. Possible parameters are specified on the

MathKernel page.

#!/usr/local/bin/MathematicaScript -pwfile "file" -script

Interpreter line using additional parameters.

The

*Mathematica* script does not need to have the

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

*Mathematica* script launches its own copy of the

MathKernel, which do not share variables or definitions. Note that running

*Mathematica* 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

*Mathematica* session. The

Get command will normally ignore the first line of the script if it starts with the

#! characters.

### Script Parameters

When running a

*Mathematica* script, you may often want to modify the behavior of the script by specifying parameters on the command line. It is possible for the

*Mathematica* code to access parameters passed to the

*Mathematica* script via

$ScriptCommandLine.

#!/usr/local/bin/MathematicaScript -script

(* generate "num" samples of of a mixed distribution *)

num = ToExpression[$ScriptCommandLine[[2]]];

Print /@ RandomVariate[

MixtureDistribution[

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

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.