Skip to content

RunProcess

Runs a program to completion and reports its exit code and output. A string names the program; a list gives the program and its arguments. The program is started directly, not through a shell.

$ wo 'RunProcess[{"echo", "hello"}]'
<|ExitCode -> 0, StandardOutput -> hello
, StandardError -> |>

A property name picks one of the results:

$ wo 'RunProcess[{"sh", "-c", "exit 3"}, "ExitCode"]'
3
$ wo 'RunProcess[{"sh", "-c", "echo oops >&2"}, "StandardError"]'
oops

A third argument is fed to the program's standard input:

$ wo 'RunProcess[{"tr", "a-z", "A-Z"}, "StandardOutput", "shout"]'
SHOUT

ProcessDirectory runs the program in a directory, and ProcessEnvironment gives it its environment variables:

$ wo 'RunProcess[{"sh", "-c", "pwd"}, "StandardOutput", ProcessDirectory -> "/"]'
/

A ProcessEnvironment replaces the environment outright, search path included, so the program is looked up in its PATH — an absolute name needs none:

$ wo 'RunProcess[{"/bin/sh", "-c", "echo $GREETING"}, "StandardOutput", ProcessEnvironment -> <|"GREETING" -> "hi"|>]'
hi

A program that cannot be found gives $Failed:

$ wo 'RunProcess["no-such-program-anywhere"]'

RunProcess::pnfd: Program no-such-program-anywhere not found. Check Environment["PATH"].
$Failed