This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
 Documentation / Mathematica / The Mathematica Book / Practical Introduction / Files and External Operations  /

1.11.10 Running External Programs

Although Mathematica does many things well, there are some things that are inevitably better done by external programs. You can use Mathematica to control the external programs, or to analyze output they generate.
On almost all computer systems, it is possible to run external programs directly from within Mathematica. Mathematica communicates with the external programs through interprocess communication mechanisms such as pipes.
In the simplest cases, the only communication you need is to send and receive plain text. You can prepare input in Mathematica, then give it as the standard input for the external program. Or you can take the standard output of the external program, and use it as input to Mathematica.
In general, Mathematica allows you to treat streams of data exchanged with external programs just like files. In place of a file name, you give the external command to run, prefaced by an exclamation point.


Some ways to communicate with external programs.

  • This feeds the expression x^2+y^2 as input to the external command lpr, which, on a typical Berkeley Unix system, sends output to a printer.
  • In[1]:= x^2 + y^2 >> "!lpr"

  • With a text-based interface, putting ! at the beginning of a line causes the remainder of the line to be executed as an external command. squares is an external program which prints numbers and their squares.
  • In[2]:= !squares 4

    1 1
    2 4
    3 9
    4 16

  • This runs the external command squares4, then reads numbers from the output it produces.
  • In[3]:= ReadList["!squares 4", Number, RecordLists->True]

    Out[3]=