6

I'd like to write command line scripts in Mathematica, but I can't seem to find an Argv[i_Integer] like function. (The docs are FANTASTIC otherwise.)

tshepang
  • 12,111
  • 21
  • 91
  • 136
mcandre
  • 22,868
  • 20
  • 88
  • 147
  • 2
    similar question: [If doing “math foo bar”, how to get foo and bar inside Mathematica?](http://stackoverflow.com/q/7149252/211232) – WReach Nov 01 '11 at 22:29

3 Answers3

6

It seems that $ScriptCommandLine is the variable you would need for this, assuming you're using MathematicaScript for scripting. If you use math or MathKernel -script it would be $CommandLine.

Other relevant discussions: a rather old one and one in the MMA toolbag.

Community
  • 1
  • 1
Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
4

$CommandLine works for Mathematica, MathematicaScript, MathKernel, and math.

args.ma:

#!/usr/bin/env MathKernel -script

Print[$CommandLine]

Example:

$ ./args.ma a b c
{"MathKernel", "-script", "./args.ma", "a", "b", "c"}
mcandre
  • 22,868
  • 20
  • 88
  • 147
1

I'm fond of passing things in using environment variables:

http://reference.wolfram.com/mathematica/ref/Environment.html

Joshua Martell
  • 7,074
  • 2
  • 30
  • 37