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.

Structured communicationuse WSTP to exchange expressions with WSTPcompatible external programs
Unstructured communicationuse file reading and writing operations to exchange ordinary text

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.

<<fileread in a file
<<"!command"run an external command, and read in the output it produces
expr>>"!command"feed the textual form of expr to an external command
ReadList["!command",Number]run an external command, and read in a list of the numbers it produces

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 FactorInteger to the external program lpr. On many Unix systems, this program generates a printout:
Click for copyable input
This executes the external command echo$TERM, then reads the result as Wolfram Language input:
Click for copyable input
With a textbased 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:
Click for copyable input

One point to notice is that you can get away with dropping the double quotes around the name of a pipe on the righthand 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 twoway 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.

OpenWrite[]open a new file with a unique name in the default area for temporary files on your computer system

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 Run.

Run["command",arg1,]run an external command from within the Wolfram Language

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:
Click for copyable input

Note that when you use Run, you must not preface commands with exclamation points. 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 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, 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.

RunThrough["command",expr]run command, using expr as input, and reading the output back into the Wolfram Language

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 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 RunThrough, like 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:
Click for copyable input
SystemOpen["target"]opens the specified file, URL or other target with the associated program on your computer system

Opening files with external programs.

This opens the URL using your system's preferred web browser:
Click for copyable input

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.