External Programs

On most computer systems, you can execute external programs or commands from within the Wolfram Language. Often you will want to take expressions you have generated in the Wolfram Language, and send them to an external program, or take results from external programs, and read them into the Wolfram Language.

The Wolfram Language supports two basic forms of communication with external programs: structured and unstructured.

Two kinds of communication with external programs in the Wolfram Language.

The idea of structured communication is to exchange complete Wolfram Language expressions to external programs which are specially set up to handle such objects. The basis for structured communication is the Wolfram Symbolic Transfer Protocol (WSTP) system, discussed in "WSTP and External Program Communication".

Unstructured communication consists in sending and receiving ordinary text from external programs. The basic idea is to treat an external program very much like a file, and to support the same kinds of reading and writing operations.

Some ways to communicate with external programs.

In general, wherever you might use an ordinary file name, the Wolfram Language allows you instead to give a pipe, written as an external command, prefaced by an exclamation point. When you use the pipe, the Wolfram Language will execute the external command, and send or receive text from it.

This sends the result from (RefLink[FactorInteger, paclet:ref/FactorInteger]) to the external program lpr. On many Unix systems, this program generates a printout:

FactorInteger[2^31-1]>>!lpr

This executes the external command echo$TERM, then reads the result as Wolfram Language input:

<<"!echo $TERM"

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[1]:= !squares 4

1  1
2  4
3  9
4  16

This runs the external command squares 4, then reads numbers from the output it produces:

ReadList["!squares 4",Number,RecordLists->True]

One point to notice is that you can get away with dropping the double quotes around the name of a pipe on the right-hand side of << or >> if the name does not contain any spaces or other special characters.

Pipes in the Wolfram Language provide a very general mechanism for unstructured communication with external programs. On many computer systems, Wolfram Language pipes are implemented using pipe mechanisms in the underlying operating system; in some cases, however, other interprocess communication mechanisms are used. One restriction of unstructured communication in the Wolfram Language is that a given pipe can only be used for input or for output, and not for both at the same time. In order to do genuine two-way communication, you need to use WSTP.

Even with unstructured communication, you can nevertheless set up somewhat more complicated arrangements by using "temporary files". The basic idea is to write data to a file, then to read it as needed.

Opening a "temporary file".

Particularly when you work with temporary files, you may find it useful to be able to execute external commands which do not explicitly send or receive data from the Wolfram Language. You can do this using the Wolfram Language function (RefLink[Run, paclet:ref/Run]).

Running external commands without input or output.

This executes the external Unix command date. The returned value is an "exit code" from the operating system:

Run["date"]

Note that when you use (RefLink[Run, paclet:ref/Run]), you must not preface commands with exclamation points. (RefLink[Run, paclet:ref/Run]) simply takes the textual forms of the arguments you specify, then joins them together with spaces in between, and executes the resulting string as an external shell command.

It is important to realize that (RefLink[Run, paclet:ref/Run]) never "captures" any of the output from an external command. As a result, where this output goes is purely determined by your operating system. Similarly, (RefLink[Run, paclet:ref/Run]) does not supply input to external commands. This means that the commands can get input through any mechanism provided by your operating system. Sometimes external commands may be able to access the same input and output streams that are used by the Wolfram Language itself. In some cases, this may be what you want. But particularly if you are using the Wolfram Language with a front end, this can cause considerable trouble.

Running Wolfram Language expressions through external programs.

As discussed above, << and >> cannot be used to both send and receive data from an external program at the same time. Nevertheless, by using temporary files, you can effectively both send and receive data from an external program while still using unstructured communication.

The function (RefLink[RunThrough, paclet:ref/RunThrough]) writes the text of an expression to a temporary file, then feeds this file as input to an external program, and captures the output as input to the Wolfram Language. Note that in (RefLink[RunThrough, paclet:ref/RunThrough]), like (RefLink[Run, paclet:ref/Run]), you should not preface the names of external commands with exclamation points.

This feeds the expression 789 to the external program cat, which in this case simply echoes the text of the expression. The output from cat is then read back into the Wolfram Language:

RunThrough["cat",789]

Opening files with external programs.

This opens the URL using your system's preferred web browser:

SystemOpen["http://www.wolfram.com"]

(RefLink[SystemOpen, paclet:ref/SystemOpen]) uses settings in your operating system to determine how to open a URI or file. When opening files, it typically uses the same program that would be used if you double-clicked the file's icon.