If you encounter the following error when attempting to execute a shell script on macOS:

zsh: operation not permitted: ./{script-name}

It typically indicates that your Zsh shell is blocking the execution of the script due to permission or security issues.

🔎 Common problems and how to fix them

✅ 1. Missing Execution Permission

It is most likely that the script is not marked as executable. You can fix this by giving the execution permission:

chmod +x ./{script-name}

Try running the script again after the permission is set:

./{script-name}

✅ 2. macOS security controls (Gatekeeper)

As part of its Gatekeeper security feature, macOS may block scripts that were downloaded from the internet. Run the following command to remove the quarantine attribute from the script:

xattr -d com.apple.quarantine ./{script-name}

This will let the program run without macOS stopping it.


✅ 3. Incorrect shebang

Make sure that your script specifies the correct interpreter (called a ‘shebang’) in the script.

For Bash:

#!/bin/bash

For Zsh:

#!/bin/zsh

The shebang instructs the system which shell to use when executing the script. It’s possible that the script won’t run if it doesn’t have it.


✅ 4. Allow the script in macOS Preferences manually

If the script continues to be blocked by macOS, it can be manually enabled through the System Preferences:

  1. Go to System Preferences > Security & Privacy > General.
  2. If you see a message saying “Script was blocked”, click Allow Anyway.

📋 Summary of key commands

Here are the commands that can resolve the problem.

  1. Grant execution permission:
chmod +x ./{script-name}
  1. Remove the quarantine attribute:
xattr -d com.apple.quarantine ./{script-name}
  1. Run the script:
./{script-name}

🧑 Conclusion

The zsh: operation not permitted error is often caused by missing permissions or macOS security settings. Following the instructions above, you should be able to execute your shell script without any issues.

You should understand how macOS Gatekeeper works and how to handle script permissions effectively if you frequently work with shell scripts.

Happy scripting! 🚀