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 #
| Parameter | Type | Required | Values | What it does |
|---|---|---|---|---|
script | text | Yes | Required. The shell code. | |
input | text | Text sent to the script's standard input. Supports {{ }}. | ||
into | text | The variable that receives the output text. | ||
sample | any value | What into holds during a dry run. See Gotchas. | ||
shell | one of | zsh bash sh | zsh, bash or sh. Anything else fails the step. | |
timeout | number | Seconds before the step is cancelled. Also settable on the step itself. |
Every step also accepts label, onFailure, timeout and dryRunSafe.
Examples #
- Run shell script “ls ~/Desktop | wc -l | tr -d ' '”
- Show “{{count}} items on the Desktop”
- Run shell script “tr a-z A-Z”
- Show “{{loud}}”
Gotchas #
Dry run. A dry run never runs the script. It stores
sampleininto(an empty string if you gave nosample) so later steps still have a value, and the trace shows "dry run: not executed". Give a realisticsamplefor any step whose output a later step uses.Timeout. A step is cancelled after 10 s unless you set
timeouton 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 needstimeout; 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 onPATH. 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 likepasswordandtoken, not text inside a script). Read secrets from a file or the Keychain at run time instead, for examplesecurity 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