"Shell" (External Evaluation System)

Details

  • On macOS and Linux platforms, most shells are supported, including sh, bash, zsh, csh. On Windows, the Windows command prompt and PowerShell are supported.

ExternalEvaluate Usage

  • ExternalEvaluate["Shell",code] executes a string in an operating system shell and returns the result as a Wolfram Language expression.
  • ExternalEvaluate["Shell"form,code] executes the command string and returns the result in the specified form. Possible specifications for form are "Expression", "StandardOutput", "StandardError", "ExitCode" and "Association".

Usage Notes

  • String templates (<**>) can be used to evaluate and insert Wolfram Language expressions into the shell code string.

Examples

open allclose all

Basic Examples  (1)

Run a command in your default shell and return the result:

If a command "ExitCode" is not 0, a Failure object is returned:

Type > to get a Shell code cell that uses ExternalEvaluate to evaluate:

echo $(date)

Scope  (3)

Start a session:

Evaluate a command and return the result:

Extract the "ExitCode" from the result:

Extract the original command from the result:

Extract the "StandardOutput" and "StandardError" from the result:

Close the session:

Use "ReturnType" to specify a different return type:

Use File to run arbitrary binary or script files:

Applications  (1)

Get your local network address:

ifconfig en0 | awk '$1 == "inet" {print $2}'

Use Spotlight to search for specific files on your Mac:

mdfind PersonalNotebook

Show the 10 largest open files:

lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB" " " $9 " " $1 }' | sort -n -u | tail

Properties & Relations  (2)

Use FindExternalEvaluators to find all available targets in your machine:

Use "Target" to manually specify a shell on your machine:

Powershell.exe is a valid "Target" on a Windows system:

RunProcess can also be used to execute shell commands, but it does not print output interactively:

Possible Issues  (2)

ExternalEvaluate is unable to interrupt if the program is asking for input:

When running external commands that are requiring user input, it is advised to run them in a non-interactive mode:

ExternalEvaluate is printing output in real time:

$Output can be used to suppress Print output: