How to Fix ‘zsh: permission denied’ Error on Mac

When running a shell script, compiled binary, or command-line utility in the macOS Terminal, encountering the error message zsh: permission denied: [filename] prevents the script from executing. Since macOS Catalina, Apple has utilised the Z shell (zsh) as the default command interpreter. When zsh attempts to invoke an executable file that lacks execute rights, belongs to a different administrative user, or has been quarantined by Apple’s Gatekeeper subsystem, the kernel immediately blocks execution and returns a permission denial status code.

Why Does Zsh Report Permission Denied in macOS?

Understanding the root cause allows you to apply the appropriate Unix permission or security entitlement without compromising operating system integrity:

  • Missing Executable Permission Bits: Newly created or downloaded shell scripts (such as .sh, .command, or Python scripts) default to read and write permissions (-rw-r--r--) without the required execute bit (x).
  • Accidental Directory Execution: Attempting to run a folder path directly rather than an executable script contained within that folder.
  • File Ownership Mismatch: Files created under the root account (e.g. via sudo) or transferred from another user account cannot be invoked by your active standard account.
  • macOS Gatekeeper Quarantine Attribute: Scripts or binaries downloaded from the internet via web browsers or Slack are tagged with the com.apple.quarantine extended attribute.
  • Missing Terminal Full Disk Access: macOS Transparency, Consent, and Control (TCC) security restrictions preventing Terminal from accessing protected user directories such as Desktop, Documents, or external volumes.

Step 1: Grant Executable Rights Using chmod +x

In standard Unix environments, a file cannot be executed directly unless its file mode includes the execution flag for the current user.

  1. Open Terminal from Applications > Utilities (or via Spotlight with Cmd + Space).
  2. Check the existing permissions of your file by running:
    ls -l /path/to/your/script.sh
  3. If the output displays -rw-r--r--, the file lacks execution privileges.
  4. Grant executable rights to the current user by entering:
    chmod +x /path/to/your/script.sh
  5. Verify the updated permissions: the file attributes will now display -rwxr-xr-x.
  6. Execute your script directly:
    ./path/to/your/script.sh

Step 2: Explicitly Invoke the Desired Shell Interpreter

If you prefer not to alter the file’s permission flags, you can invoke the script by passing it as an argument directly to the appropriate shell binary.

  1. For bash scripts, run:
    bash script.sh
  2. For zsh scripts, run:
    zsh script.sh
  3. For Python scripts, run:
    python3 script.py
  4. This method bypasses the direct execute bit requirement because the shell binary itself already possesses execution privileges and reads your script as plain text input.

Step 3: Fix File Ownership with chown

If the target file was generated using elevated administrative commands, it may belong to root rather than your personal macOS user account.

  1. Check who owns the file:
    ls -l filename
  2. If the owner is listed as root or another username, reclaim ownership by running:
    sudo chown $(whoami):staff /path/to/your/script.sh
  3. Enter your macOS administrator password when prompted.
  4. Ensure read, write, and execute permissions are set for your user:
    chmod 755 /path/to/your/script.sh

Step 4: Strip Apple Quarantine Extended Attributes

Files downloaded via Safari, Chrome, or curl frequently receive quarantine markers that prevent silent local terminal execution.

  1. Inspect whether the file possesses extended security attributes:
    xattr -l /path/to/your/script.sh
  2. If the output shows com.apple.quarantine, remove this attribute by running:
    xattr -d com.apple.quarantine /path/to/your/script.sh
  3. To recursively clear quarantine flags across an entire downloaded development directory, use:
    xattr -dr com.apple.quarantine /path/to/folder/

Step 5: Verify You Are Not Executing a Directory Path

If your command target is a directory rather than a file, zsh returns permission denied because directories cannot be directly executed as binaries.

  1. Run the file command against your target:
    file /path/to/target
  2. If the output states directory, ensure you append the specific executable binary or script inside that directory (e.g., /path/to/target/run.sh).

Step 6: Grant Full Disk Access to Terminal in macOS System Settings

If the script attempts to read or write files within protected user folders (Documents, Downloads, or Network Volumes), macOS TCC policies block execution.

  1. Open System Settings from the Apple menu.
  2. Select Privacy & Security on the left sidebar.
  3. Click Full Disk Access in the right-hand panel.
  4. Toggle the switch next to Terminal (or iTerm2) to the On position.
  5. If prompted, enter your administrator password and click Quit & Reopen to apply the security policy.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.