
You can run a shell script from a Mac shortcut by adding the Run Shell Script action in the Shortcuts app. Create a new shortcut, add the action, paste or type your script, choose the correct shell and input settings, then run the shortcut or assign it to a keyboard shortcut, menu bar button, or other trigger.
Quick Answer
- Open Shortcuts on your Mac. 2. Select New Shortcut. 3. Search for and add Run Shell Script. 4. Enter your shell commands. 5. Choose a shell, such as /bin/zsh, and set Pass Input to the option your script requires. 6. Name and save the shortcut. 7. Click the Run button to test it.
What You Need Before You Start
A shell script is a text file containing commands that macOS can execute. A shortcut is an automation workflow created in Apple’s Shortcuts app. The Run Shell Script action connects the two: it lets a shortcut send commands to a shell, such as Z shell (zsh), and use the result in later shortcut actions.
You do not need to create a separate .sh file. For a simple script, you can type the commands directly into the action. This is often the easiest option for tasks such as:
- Creating or moving files
- Renaming groups of files
- Starting an application
- Running a command-line utility
- Checking a folder or system condition
- Sending the output of a command to another shortcut action
If you already have a script file, you can also call it from the shortcut. Make sure you understand what the script does before running it, especially if it deletes, overwrites, or moves files.
Warning: A shell script can change files and system settings without showing a confirmation for every action. Test scripts with harmless sample files first, and keep backups of important data.
Method 1: Add the Shell Script Directly to a Shortcut
This method is best when the script is short or you want to keep the entire automation in one shortcut.
1. Create a new shortcut
- Open Shortcuts. You can find it in the Applications folder or open it with Spotlight.
- Click New Shortcut in the toolbar.
- Click the shortcut name at the top, enter a descriptive name, and press Return.
Use a name that explains the task, such as “Create Daily Folder” or “Clean Temporary Files.”
2. Add the Run Shell Script action
- In the action library, click the search field.
- Search for Run Shell Script.
- Double-click the action, or drag it into the shortcut workflow.
The action includes a script editor and settings for the shell and input.
3. Enter a script
For example, this script creates a folder on your Desktop:
mkdir -p "$HOME/Desktop/Shortcut Test"
echo "Folder created."
The mkdir -p command creates the folder. The -p option prevents an error if the folder already exists. $HOME represents your macOS user folder, so this command works without manually entering your account name.
To run the script:
- Click inside the script area.
- Replace the sample text with your commands.
- Click the Run button at the top of the shortcut editor.
If the script produces text, Shortcuts may display the result or pass it to the next action.
4. Choose the shell
The Shell menu determines which command interpreter runs the script. For most current Mac scripts, /bin/zsh is a practical default. Some older scripts were written for /bin/bash.
Choose the shell that matches the script:
- Use /bin/zsh for scripts written for Z shell.
- Use /bin/bash for scripts that depend on Bash-specific syntax.
- Avoid changing this setting unless you know which shell the script requires.
Shells can interpret commands differently. A script that works in Bash may need changes before it works in Z shell.
5. Configure input
The Pass Input menu controls what happens to data received from the previous shortcut action. Common choices include:
- to stdin: Sends the input to the script’s standard input stream. This is useful when the script reads incoming text or file data.
- as arguments: Passes the input as command-line arguments. This is often useful when the script receives selected files.
- ignore input: Runs the script without using data from earlier actions.
If your shortcut starts with no other action, choose ignore input unless the script specifically expects input.
6. Save and test the shortcut
Shortcuts saves changes automatically. Click the Run button to test it.
Test with a noncritical folder or a harmless command first. If the script creates a file, check that it appears in the intended location. If it does not work, read the error shown below the action and review the troubleshooting section later in this guide.
Method 2: Run an Existing Shell Script File
If you already have a .sh file, you can run it from a shortcut instead of copying its contents into the Run Shell Script action.
1. Store the script in a stable location
Place the script in a folder that will not be moved or renamed. Your home folder can be useful for personal scripts. Avoid storing an automation script in a temporary download folder.
A script file might be named:
backup-notes.sh
2. Make the script executable
Open Terminal and run the following command, replacing the example path with the path to your script:
chmod +x "$HOME/Scripts/backup-notes.sh"
This adds execute permission for the file owner. It does not run the script.
To reverse this permission change later, run:
chmod -x "$HOME/Scripts/backup-notes.sh"
If the Scripts folder does not exist, create it in Finder or with:
mkdir -p "$HOME/Scripts"
The command creates a Scripts folder in your home folder. To remove that empty folder later, use Finder or run:
rmdir "$HOME/Scripts"
Do not use rm -rf casually. It can permanently delete a folder and everything inside it.
3. Add the script path to Shortcuts
- Open Shortcuts and create or edit a shortcut.
- Add the Run Shell Script action.
- Enter the full path to the script, for example:
"$HOME/Scripts/backup-notes.sh"
You can also call the script explicitly with its shell:
/bin/zsh "$HOME/Scripts/backup-notes.sh"
Using the shell explicitly can help when the script is not marked as executable. The script should still begin with a suitable interpreter line, such as:
#!/bin/zsh
That first line is called a shebang. It tells macOS which interpreter should run the file when it is executed directly.
4. Test the existing script
Run the shortcut while watching for:
- Permission prompts
- Missing files or folders
- Incorrect paths
- Output or error messages
- Unexpected changes to your data
If the script works in Terminal but not in Shortcuts, the two environments may have different permissions or environment variables. Use full paths instead of relying on shortcuts such as ~ inside complex commands, and request access to the necessary files when macOS displays a prompt.
Pass Selected Files or Folders to a Script
A useful shortcut can operate on files you select in Finder. The general workflow is:
- Add a shortcut action that receives files or folders.
- Add Run Shell Script.
- Set Pass Input to as arguments.
- Reference the arguments in the script.
For example:
for item in "$@"; do
echo "Selected: $item"
done
"$@" represents each input item as a separate argument. The quotation marks are important because they preserve spaces in file and folder names.
You can replace echo with an action that processes each item. For example, a script could rename, copy, or inspect selected files. Test such shortcuts with copies first.
If the shortcut is not available from Finder, open the shortcut’s details and look for options related to using it as a Quick Action or from the Services menu. The exact options shown can vary depending on the shortcut and the macOS configuration.
Run the Shortcut with a Keyboard Shortcut or Other Trigger
After the shell script works, you can make it easier to launch.
In the shortcut’s details, look for options that let you:
- Assign a keyboard shortcut
- Add the shortcut to the menu bar
- Use it from the Services menu
- Add it to the Dock
- Run it with Siri or another supported automation trigger
Choose a keyboard combination that does not conflict with an existing macOS or app shortcut. If another app already uses the same combination, the shortcut may not run consistently.
You can also run the shortcut manually from the Shortcuts app whenever you need it. This is the safest way to test changes before adding an automatic trigger.
Permissions and Privacy Prompts
A shortcut running a shell script may need access to files, folders, or applications. macOS can ask for permission the first time the shortcut attempts an action.
For example, a script may need access to:
- Your Desktop, Documents, or Downloads folder
- An external drive
- Another application
- Files stored in a protected location
Read each prompt carefully. If you deny access, the shortcut may fail even though the commands are correct.
Avoid weakening system security just to make a script work. Instead, revise the script so it uses a permitted location or grant access only to the folder it genuinely needs. If the shortcut performs a sensitive operation, do not run it while logged in to an account that has more access than necessary.
Troubleshooting
“Run Shell Script” does not appear
Confirm that you are searching in the action library within Shortcuts, not Spotlight or Finder. Search for shell or Run Shell Script. If you are editing an existing shortcut, click an empty area in the workflow before adding the action.
The shortcut runs but nothing happens
Check whether the command only produces output rather than a visible result. Add a simple test command:
echo "The shortcut ran successfully."
You can also add a shortcut action after Run Shell Script that displays or uses the result.
Verify every path. A command can run successfully but affect a different folder than you expected if a relative path is used. Full paths are more reliable in automation.
“Permission denied” appears
The script may lack execute permission, or macOS may be blocking access to a protected folder.
For an existing script, add execute permission:
chmod +x "$HOME/Scripts/backup-notes.sh"
To undo it:
chmod -x "$HOME/Scripts/backup-notes.sh"
If the error concerns a folder such as Documents or an external drive, review the permission prompt and the relevant privacy settings in System Settings. Do not grant broad access when a narrower location will work.
The command works in Terminal but fails in Shortcuts
Shortcuts may run with a different working folder and a more limited environment than an interactive Terminal session. Fix common causes by:
- Using full paths to commands and files
- Avoiding assumptions about the current directory
- Defining needed variables inside the script
- Choosing the correct shell
- Checking permissions
For example, instead of relying on a command found through your interactive shell configuration, use its full path when appropriate.
The script receives incorrect file names
Set Pass Input to as arguments and use "$@" in the script. Do not combine all arguments into one unquoted variable, because spaces and special characters in file names can cause errors.
A script deletes or changes the wrong files
Stop testing immediately. Review every path and command before running the shortcut again. Replace destructive commands with harmless output commands while testing. For example, use:
printf '%s\n' "$@"
This prints the received paths without changing them.
Warning: Commands such as
rm,mv, andcpcan permanently delete, move, or overwrite data. Confirm the target path and test with duplicate files before using them in a shortcut.
Frequently Asked Questions
Can I run a .sh file directly from Shortcuts?
Yes. Add Run Shell Script and enter the script’s full path, or call it with /bin/zsh or /bin/bash. The file must be stored in a stable location and may need execute permission.
Which shell should I choose?
Use the shell the script was written for. /bin/zsh is a sensible choice for a new script unless it requires Bash-specific features. Older scripts may need /bin/bash.
Can a shortcut pass Finder files to a shell script?
Yes. Add an input action, set Pass Input to as arguments, and process the files with "$@". Always quote file paths so spaces do not break the script.
Does running a shell script from Shortcuts require Terminal?
No. The Run Shell Script action runs the commands without requiring you to open a Terminal window. Terminal is still useful for testing commands, changing file permissions, and diagnosing errors.
Why does macOS ask for permission?
The shortcut or script is trying to access a protected file, folder, drive, or application. Approve only the access needed for the task. If you deny it, the shortcut may not be able to complete.
Final Thoughts
The Run Shell Script action makes it straightforward to combine Mac command-line tasks with Shortcuts automation.