Sub topic 11
Calling shell scripts from a shortcut
This chapter shows how to run a shell script from a shortcut with the Run Shell Script action. It assumes you can build a multi-step shortcut, use variables, and receive input with Shortcut Input. Basic Terminal use helps but is not required.
Why call a shell script
Shortcuts covers many tasks with built-in actions. Some jobs are easier in the shell: transforming text with standard tools, counting bytes in files, or calling a command-line program you already have. The Run Shell Script action lets a shortcut hand data to a script and take the script's output back as the next step's input.
The Run Shell Script action
Add Run Shell Script from the action list. It has these main settings:
- Shell: zsh is the default on current macOS. You can also choose bash, sh and others.
- Input: the data from the previous action, or a variable you drop in.
- Pass Input: either to stdin or as arguments.
- Run as Administrator: leave this off unless the script truly needs it.
The script's standard output becomes the action's result. Later actions can use that result through the magic variable Shell Script Result.
Passing input to stdin
With to stdin, the input text arrives on standard input. Programs such as tr, sed and grep read from stdin by default, so they work with no extra wiring. This script converts incoming text to uppercase:
tr '[:lower:]' '[:upper:]'To try it, build this shortcut:
- Add Ask for Input with the prompt "Text to convert".
- Add Run Shell Script, set Shell to bash, Input to Provided Input, and Pass Input to to stdin.
- Paste the script above.
- Add Show Result, and choose the Shell Script Result variable.
Run it and type hello. The result window shows HELLO.
Passing input as arguments
When the input is files, choose as arguments. Each item becomes one positional argument, available as "$@". This script prints the size in bytes of each file it receives:
for f in "$@"; do
wc -c < "$f"
doneQuote "$f" every time. Without quotes, a file named My Notes.txt splits into two words and the command fails.
For a quick test, add Get File or Select File before the script action, so the file feeds into it. For a Finder Quick Action, set the shortcut to receive files, and the same script works on whatever you selected.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Result is empty | The script wrote to stderr or printed nothing | Add `echo` to confirm output, and check for error text |
| Command not found | The shortcut's shell has a smaller `PATH` than Terminal | Use the full path, such as `/usr/bin/python3`, or `/opt/homebrew/bin/...` for Homebrew tools |
| Script ignores input | Pass Input is set to the wrong mode | Use stdin for `tr` and similar tools, arguments for file loops |
| Filename with spaces breaks | Missing quotes around the variable | Write `"$f"` instead of `$f` |
| Permission error on a folder | macOS blocked access to a protected location | Approve the prompt, or review the shortcut's permissions in the Shortcuts settings |
Security considerations
A shell script has far more reach than most actions. Read any script in a shortcut you download before you run it. Avoid Run as Administrator unless you understand why it is needed. Never build commands by pasting untrusted text into a script string. Pass that text as input instead, so the shell reads it as data.
Exercise
A shortcut passes three files to a Run Shell Script action set to "as arguments". Your script is wc -c < "$1". How many files does it measure, and how would you change it to measure all three?
Show answer
It measures only the first file, because $1 is the first argument. Use a loop over all arguments: for f in "$@"; do wc -c < "$f"; done.
Exercise
You run tr '[:lower:]' '[:upper:]' in a Run Shell Script action, but the result is empty. Pass Input is set to "as arguments". What is wrong and how do you fix it?
Show answer
tr reads standard input, and with "as arguments" nothing is sent to stdin. Change Pass Input to "to stdin".