To check whether a shell script succeeded in a Mac shortcut, capture its exit status immediately after it runs and pass that number back as text. In Shortcuts, add the Run Shell Script action, then use an If action to branch on the returned value: 0 means success by the shell convention; a nonzero value indicates an error or other unsuccessful result. This method makes the status explicit instead of assuming the shortcut exposes a separate status variable.
Add a shell action and capture the status
Open the shortcut in the Shortcuts app on your Mac. Search the action library for Run Shell Script and add it. Apple describes shell-script actions as part of the Mac Shortcuts ecosystem, though the Shortcuts User Guide does not document every setting or version detail for this action. If it is absent from your action library, don’t substitute Automator’s similarly named action and assume it behaves identically; check the actions available on your Mac. Apple’s Shortcuts overview
In the script field, put the script command on one line, then capture $? on the very next line. For example, if the script is executable and located at the illustrative path shown below:
/Users/yourname/Scripts/refresh-files.sh >/dev/null
script_status=$?
printf '%s' "$script_status"
Replace the example path with the actual path to your script. The redirection sends the script’s standard output to /dev/null, so the action’s text output contains just the status number. Remove >/dev/null if you want the script’s output too, but then the returned text may include both that output and the status. Standard error is not suppressed by this redirection.
$? holds the exit status of the most recently completed command. That is why the assignment must come directly after the script invocation: if another command runs first, $? will describe that command instead. printf then emits the saved number as text, which Shortcuts can use as action output. Apple’s Mac shell-script workflow guide
Branch on the returned number
Add an If action after Run Shell Script. Set its input to the shell action’s output, choose the text condition is, and enter 0. Place the success actions under If and any failure handling under Otherwise. The Mac Shortcuts If action routes content down one of two paths based on the condition you configure. Apple’s guide to If actions in Shortcuts on Mac
Treat the result as text because the shell action prints it. A successful run should return 0; a nonzero result should take the other branch. You can put a Show Alert action in the failure branch and include the status value in its message, or pass the value to a later action for logging. Shell scripts often use different nonzero values to distinguish failure causes, so preserving the number can help with diagnosis.
Avoid common status-checking mistakes
Do not put echo $? on a later action and expect it to refer to the earlier shell process. Each shell action starts its own command execution; $? is meaningful inside the same shell run and immediately after the command whose status you want. Likewise, printing a status before the script runs only prints the result of whatever ran previously.
If the script performs multiple commands, its final exit status may depend on how the script is written. For example, a later successful command can mask an earlier failure unless the script deliberately propagates or handles that failure. The shortcut can only branch on the status the script ultimately returns. Review the script’s own error handling if the result does not reflect the step you care about.
When you run a shortcut from Terminal
If your goal is to check whether the entire shortcut ran successfully from Terminal, Apple documents a separate shortcuts command: shortcuts run "Shortcut Name". That command exits 0 when the shortcut run succeeds and 1 on error. This is the status of the shortcut invocation, not a dedicated variable containing the internal shell action’s exit code. To use the script’s own status in a shortcut branch, capture and print it inside the shell action as described above. Apple’s command-line guide for Shortcuts on Mac
The practical pattern is simple: run the script, save $? immediately, print that saved value, and compare the resulting text with 0 in an If action. That gives the Mac shortcut a clear success path and a failure path while keeping the decision tied to the script’s actual exit result.
Sources
Get More Done With Shortcuts! — Apple
Use a shell script action in an Automator workflow on Mac — Apple Support
Use If actions in Shortcuts on Mac — Apple Support
Run shortcuts from the command line — Apple Support