sodcalculator

10 Tech How-To

Fix zsh Permission Denied on Mac Terminal

Guide 10-59187 · Updated · Step by step

If you’re seeing “zsh: permission denied” when trying to run a script or command in the Mac Terminal, it usually means the file doesn’t have execute permissions, or macOS is blocking it for security reasons. This guide walks you through the quick fixes, from changing file permissions to clearing extended attributes, so you can get back to work.

What You’ll Need

  • A Mac running macOS Catalina (10.15) or later, where zsh is the default shell.
  • The full path to the file or command that’s giving the error.
  • Administrator access if the file is in a system-protected location (you may need to use sudo).

Step-by-Step

  1. Identify the file or command. Look at the error message. It usually says something like zsh: permission denied: ./script.sh. Note the exact path and name.
  2. Check the current permissions. In Terminal, run ls -l /path/to/file. You’ll see a string like -rw-r--r-- at the start. The first character is the file type (- for regular file, d for directory). The next nine characters are permissions: three for the owner, three for the group, three for others. The x character means execute. If there’s no x in the owner’s group (e.g., -rw-r--r--), the file isn’t executable.
  3. Make the file executable. Run chmod +x /path/to/file. This adds execute permission for the owner, group, and others. If you only want the owner to execute it, use chmod u+x /path/to/file.
  4. Try running the command again. Use /path/to/file or ./file if you’re in the same directory. It should now run, unless there’s another issue (see troubleshooting below).
  5. If it still fails, check for extended attributes. macOS may quarantine downloaded files. Run xattr -l /path/to/file to list attributes. If you see com.apple.quarantine, remove it with xattr -d com.apple.quarantine /path/to/file.
  6. If the file is in a protected location (like /usr/bin or /System), you may need to use sudo chmod +x /path/to/file. Enter your password when prompted. Be careful—only do this for files you trust.
  7. If you’re trying to run a command that’s not a file (like mycommand), it might be that the command isn’t in your PATH. Check with which mycommand. If it’s not found, you may need to install it or add its directory to your PATH in ~/.zshrc.
  8. Restart Terminal or run source ~/.zshrc after making PATH changes to apply them.

Common Mistakes

  • Using chmod 777 unnecessarily. This gives everyone full permissions, which is a security risk. Use chmod +x or chmod 755 instead.
  • Forgetting to include ./ for files in the current directory. zsh won’t run a file just by typing its name unless it’s in your PATH. Use ./script.sh.
  • Editing the wrong file. Make sure you’re changing permissions on the actual script, not a symlink or a copy.
  • Ignoring the quarantine attribute. Even after chmod +x, a downloaded file may still be blocked by Gatekeeper. Always check xattr if the error persists.
  • Using sudo on a file you don’t own. This can cause permission issues later. Try to change ownership with sudo chown $(whoami) /path/to/file if needed.

Tips

  • Use chmod 755 for scripts you want others to read and execute, but only you can write. This is a common, safe permission set.
  • Check the shebang line. For shell scripts, the first line should be #!/bin/zsh or #!/bin/bash. If it’s missing or wrong, the script may fail with a permission error even if permissions are correct.
  • If you’re writing a script, save it with Unix line endings. Windows-style CRLF can cause weird errors. Use a text editor that supports Unix line endings.
  • Add a directory to your PATH if you have many scripts you want to run from anywhere. In ~/.zshrc, add export PATH="$HOME/bin:$PATH" and put your scripts in ~/bin.
  • Use file /path/to/file to see what type of file it is. If it’s a binary, you may need to check its architecture (e.g., Intel vs. Apple Silicon).

Troubleshooting

  • Still getting “permission denied” after chmod +x? Check if the file is on a filesystem that doesn’t support Unix permissions, like a FAT32 drive. Copy it to your Mac’s internal drive and try again.
  • Error says “cannot execute binary file”? This means the file isn’t a valid executable for your Mac’s architecture. For example, an x86 binary won’t run on an ARM Mac without Rosetta 2. Install Rosetta 2 with softwareupdate --install-rosetta or get an ARM version.
  • Script runs but does nothing? Check for syntax errors by running zsh -n /path/to/file to parse without executing.
  • Permission denied on a directory? You need execute permission on the directory to enter it. Use chmod +x /path/to/directory.
  • If you’re using a package manager like Homebrew and getting permission errors, you may have installed it with the wrong ownership. Run sudo chown -R $(whoami) /opt/homebrew (or /usr/local for Intel Macs) to fix.
  • Check if System Integrity Protection (SIP) is blocking you. If you’re trying to modify files in /System or /usr, SIP may prevent it. You can’t disable SIP easily, and it’s not recommended. Instead, use your home directory or /usr/local.

Frequently Asked Questions

Why does zsh say “permission denied” even though I own the file?

Ownership doesn’t guarantee execute permission. The file may have read/write but not execute. Use chmod +x to add it.

How do I permanently allow a downloaded app to run?

Right-click the app and select “Open” from the context menu, then click “Open” in the dialog. This adds a one-time exception. For scripts, remove the quarantine attribute as shown above.

What’s the difference between chmod +x and chmod 755?

chmod +x adds execute permission to the existing permissions. chmod 755 sets the owner to read/write/execute, and group and others to read/execute. The latter is more explicit.

Can I run a script without making it executable?

Yes, you can run it with zsh /path/to/file or bash /path/to/file (depending on the shebang). This bypasses the need for execute permission.

Why does my script work in Terminal but not when double-clicked?

Double-clicking a script may open it in a text editor instead of executing it. You can set the default app to Terminal in the Get Info panel, or create an Automator app to run it.

What does “operation not permitted” mean in zsh?

This often happens when a process lacks the necessary macOS privacy permissions, like accessing files in Desktop, Documents, or Downloads. Go to System Settings → Privacy & Security → Files and Folders, and grant Terminal access.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →