Bookmark and Share

Some technical notes on setting up UNIX command-line interface environment.

Mathematica from command line

Initialization file init.m

In $UserBaseDirectory/Kernel/init.m, you can write initialization code, which will be automatically evaluated when Mathematica kernel launches. I usually put path initialization $Path, load frequently used packages, and set some global options:

PrependTo[$Path, "/path/to/mypackage/"];

<<mypackage`

SetOptions[$Options, PageWidth -> Infinity]; (* make wide lines look nicer)

Run a standalone Mathematica kernel

Mathematica has built-in support for command-line interface. At UNIX shell, do

bash$ /usr/local/bin/math
Mathematica 8.0 for macOS x86 (64-bit)
Copyright 1988-2011 Wolfram Research, Inc.

In[1]:= Sin[1+Pi]

Out[1]= -Sin[1]

More details in the Mathematica documentation:

* Mathematica Sessions, Wolfram Mathematica Documentation Center, 2011, http://reference.wolfram.com/mathematica/tutorial/MathematicaSessions.html

*

Mathematica scripts

One can make Mathematica scripts using the MathematicaScript interpreter. Make a file script.mmash

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

Print["Hello world!"]

Then run at UNIX shell

bash$ ./script.mmash 
"Hello world!"

* MathematicaKernel, Wolfram Mathematica Documentation Center, 2011, http://reference.wolfram.com/mathematica/ref/program/MathematicaScript.html

mma

rlwrap makes the command line interaction with MathematicaKernel more user friendly.

bash$ pwd
/usr/home/lumeng/bin

bash$ more mma
#!/usr/bin/env bash

rlwrap --file=/usr/local/share/rlwrap/MathematicaKernel ./math

bash$ ./mma
Mathematica 8.0 for macOS x86 (64-bit)
Copyright 1988-2011 Wolfram Research, Inc.

In[1]:= Sin[1+E]//N

Out[1]= -0.545252

In[2]:= 

Wolfram|Alpha at command line: wa

Create file wa

Examples:

$ wash us population
309 million people (world rank: 3rd) (2010 estimate)

$ wash proton mass
938.27203 MeV/c^2

$ wash -full proton mass | grep kilogram
1.672622×10^-27 kg (kilograms)
blog comments powered by Disqus