I have access to a node of a Linux cluster, on which Mathematica is installed. However, I do not have access to a Linux GUI.

Typically I write and test Mathematica scripts on my Windows personal computer (with the typical GUI/front end). Then, I run the scripts on the Linux terminal because the terminal is significantly faster than my desktop computer (my scripts read and write a lot of data). I use the command:

math < test.txt

where test.txt is a text file containing Mathematica code. I have been using this command successfully to run my scripts. I just always need to make sure that I do not try to print graphics to the screen, since I don't have a Linux GUI.

However, now I would like to write a script that prompts the user for input. For example, this code works fine on the Windows GUI:

Print["start"]; str = InputString[]; Print["string: " <> str]; Print["finish"];

But, it fails on the Linux terminal, generating this console output:

Mathematica 7.0 for Linux x86 (64-bit) Copyright 1988-2008 Wolfram Research, Inc.

In[1]:= start

In[2]:= ?

In[3]:= finish


Is there any way I can prompt the user for input on a Linux terminal? The input does not necessarily need to be interactive. For example, I can imagine there being a way to pass an input string to math by a command like:

math -input "This is my input string." < test.txt

but I am not sure what is the syntax to do this.


InputString[] works on the terminal if I simply enter a terminal using the command:


and then type InputString[] in the input prompt. However, this is not how I want to run my scripts, since this (I think) would require me to retype every command, line by line.

  • $\begingroup$ @b.gatessucks math -initfile test.txt works, but it seems that math -script test.txt does not. For me (Mathematica 7), math -script test.txt just gives me a Mathematica input prompt, but does not execute the commands in test.txt. I wonder what is the difference between -initfile and -script. $\endgroup$
    – Andrew
    Commented Sep 14, 2012 at 19:42
  • $\begingroup$ @b.gatessucks man math says that -initfile "Execute[s] the commands in the initialization file file during startup." I don't see -script described in man math. $\endgroup$
    – Andrew
    Commented Sep 14, 2012 at 19:46
  • 3
    $\begingroup$ -script may be new (?) -- it is not documented in the linux man page for v8 but it is in the GUI help. The significant difference is -script exits when done, while -initfile leaves you at a prompt (unless you Quit[] in the script of course ) $\endgroup$
    – george2079
    Commented Sep 14, 2012 at 19:49
  • $\begingroup$ @george2079 Thanks. OK, I will just use -initfile, and put Quit[] at the bottom of my scripts. $\endgroup$
    – Andrew
    Commented Sep 14, 2012 at 19:50

2 Answers 2


try: math -initfile test.txt



If you need more interactivity, take a look at mash. From the README:

This is how "math -script" should work. Namely, the same way that the perl/python/ruby/etc interpreters do:

  • take a mathematica source file as its first argument (or from stdin if no arguments),
  • make all the arguments available to the mathematica code as an array (list) called ARGV,
  • evaluate the code,
  • prints to stdout and stderr only what it is explicitly told to.

The advantages of this include having a self-contained mathematica program that can be executed (with arguments) from the command line, and be used with other programs in a pipeline, etc.

I haven't used it recently but tried it under an older Mathematica version (7 maybe?). Disadvantage is that you need perl (which should be no problem on the standard Linux machine).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.