Executing a shell script, newly compiled binary, or downloaded developer utility in the macOS Terminal only to receive the blunt error message zsh: permission denied: [filename] brings command-line productivity to a sudden halt. In macOS (where Zsh is the default login shell), this failure does not necessarily mean you lack administrative root privileges. Instead, it typically indicates that the target file lacks the POSIX execute bit (+x), that you attempted to execute a directory as a command, or that macOS Gatekeeper quarantined the file with extended attributes.
Add the Execute Permission Bit via chmod
When you create a script in a text editor or download a script file from GitHub, macOS creates it with standard read and write permissions (-rw-r--r--), omitting execution rights.
- Open Terminal on your Mac.
- Navigate to the folder holding your script, or keep its path ready.
- Check the current permissions of the file by running:
ls -la scriptname.sh - If the first column displays
-rw-r--r--, the file lacks execute permissions. - Add execute permissions for the current user by running:
chmod +x scriptname.sh - Run
ls -la scriptname.shagain to verify that the mode now reads-rwxr-xr-x. - Execute your script by prefixing it with
./:./scriptname.sh
Ensure You Are Not Executing a Directory
A frequent syntax mistake is forgetting the command binary name, causing Zsh to attempt executing an entire directory folder path.
- Inspect your terminal command carefully.
- If your command ends with a directory name (such as typing
/usr/local/bin/instead of/usr/local/bin/node), Zsh returns “permission denied” because Unix directories cannot be executed as programs. - Verify that your target file path points directly to an executable binary or script file.
Strip Gatekeeper Quarantine Extended Attributes (xattr)
Files downloaded via Safari, Chrome, or messaging apps are automatically tagged by macOS with an extended security attribute (com.apple.quarantine) that blocks shell execution.
- In Terminal, check if your file carries quarantine tags:
xattr -l scriptname.sh - If you see
com.apple.quarantinelisted, strip the attribute by executing:xattr -d com.apple.quarantine scriptname.sh - If stripping attributes from an entire directory of tools, run:
xattr -cr /path/to/folder - Attempt running the command again.
Verify File Ownership via chown
If a script or repository was cloned using sudo, root may own the file, preventing your standard macOS user account from accessing it.
- Check who owns the file:
ls -l scriptname.sh - If the owner is listed as
root, reclaim ownership for your user account:sudo chown $(whoami):staff scriptname.sh - Enter your Mac login password to confirm.
Verify the Script Shebang Line
If a shell script’s very first line points to a nonexistent interpreter, Zsh rejects execution with a misleading permission denied message.
- Open your script in Nano:
nano scriptname.sh - Inspect the top shebang line:
- For Bash: ensure it reads
#!/bin/bashor#!/usr/bin/env bash - For Zsh: ensure it reads
#!/bin/zsh - For Python: ensure it reads
#!/usr/bin/env python3
- For Bash: ensure it reads
- Ensure there are no leading spaces or invisible Windows carriage return characters (
^M) before the shebang.