Browse the docs

Run shell script

Runs a shell script with zsh, bash or sh and captures its output.

Run shell script script

Keyboard Maestro: ExecuteShellScript

Details #

Runs script with <shell> -c. The default shell is zsh. The script's standard output, with trailing newlines removed, is stdout in the step's output. If into is set, that variable gets the same text.

The output is an object: stdout, exit (always 0 on success) and, if the script wrote to standard error, stderr (first 2,000 characters). Use {{ ... }} values in the script text to pass variables in, and input to feed text to the script's standard input.

A non-zero exit fails the step. The failure message reads exit 2: followed by the first 300 characters of standard error.

Parameters #

ParameterTypeRequiredValuesWhat it does
scripttextYesRequired. The shell code.
inputtextText sent to the script's standard input. Supports {{ }}.
intotextThe variable that receives the output text.
sampleany valueWhat into holds during a dry run. See Gotchas.
shellone ofzsh bash shzsh, bash or sh. Anything else fails the step.
timeoutnumberSeconds before the step is cancelled. Also settable on the step itself.

Every step also accepts label, onFailure, timeout and dryRunSafe.

Examples #

Count files on the Desktop (runs a command)
Do
  1. Run shell script “ls ~/Desktop | wc -l | tr -d ' '”
  2. Show “{{count}} items on the Desktop”
Open in Switchboard to try it
Pass text on standard input
Do
  1. Run shell script “tr a-z A-Z”
  2. Show “{{loud}}”
Open in Switchboard to try it

Gotchas #

  • Dry run. A dry run never runs the script. It stores sample in into (an empty string if you gave no sample) so later steps still have a value, and the trace shows "dry run: not executed". Give a realistic sample for any step whose output a later step uses.

  • Timeout. A step is cancelled after 10 s unless you set timeout on the step or in its params, and never runs past what is left of the macro's own timeout (60 s unless changed). On cancel the process is stopped, then killed after 1 s if it ignores that. A long job needs timeout; a job that must outlive the macro should be started detached (nohup ... &).

  • Environment. The shell is not a login shell and does not read your terminal's .zshrc, so tools from Homebrew may not be on PATH. Use full paths such as /opt/homebrew/bin/jq.

  • Secrets. Do not paste passwords or tokens into script. The text is saved in the macro file, shown in the trace, and included when you share the macro, unless the sharing sheet's checks catch it (they match parameter names like password and token, not text inside a script). Read secrets from a file or the Keychain at run time instead, for example security find-generic-password -w -s my-service.

  • Output is read as UTF-8 text. Only trailing newlines are removed from stdout.

See also #

Run AppleScript, Run Python script, Run JavaScript, Send HTTP request