How to Fix “Permission Denied” When Running a Bash Script on macOS (2026)
The “permission denied” error when running a bash script on macOS means the file is missing its execute bit, not that the script is broken. The one-liner that fixes it:
chmod +x ./myscript.sh
Then run it again:
./myscript.sh
That works on macOS Sonoma 14 and later, including macOS 26 (Tahoe) shipped in 2025-2026. Now here is what that command actually does, and why the usual advice about “just run it with bash” is a trap.
The myth: if a script runs with bash script.sh, it is fine to leave it that way
Running bash script.sh bypasses the execute bit entirely, so it “works” while leaving the underlying file misconfigured. That is the assumption worth killing first. The interpreter reads the file as data; the kernel never checks whether the file is executable. So you get a green light in your terminal and a red light everywhere else — CI runners, cron jobs, Makefiles, Docker entrypoints, and any tool that calls the script directly.
This is the failure mode that costs people hours: the script passes locally, then fails in a GitHub Actions runner or a launchd job with the exact same “permission denied” text. The file was never made executable; the local shell was just papering over it.
Do not do this:
bash deploy.sh # hides a broken permission bit
Do this instead:
chmod +x deploy.sh
./deploy.sh # fails loudly if the bit is missing
What chmod +x actually changes on APFS
macOS stores permission bits in the file’s metadata on APFS, and chmod +x sets all three execute bits (user, group, other) in one move. You can verify with ls -l:
$ ls -l deploy.sh
-rw-r--r--@ 1 you staff 412 Nov 15 09:14 deploy.sh
$ chmod +x deploy.sh
$ ls -l deploy.sh
-rwxr-xr-x@ 1 you staff 412 Nov 15 09:14 deploy.sh
The leading - means regular file; rwxr-xr-x means owner can read/write/execute, group and others can read/execute. If you see d instead of -, you are chmod-ing a directory, which is a different problem entirely.
If you want the stricter form — only the owner gets execute — use chmod u+x deploy.sh. That is the right call for scripts in shared directories.
The second myth: “chmod +x does not stick, so macOS must be broken”
If the execute bit disappears after you move or copy the file, the problem is not macOS — it is the tool you used. The cp command in POSIX mode preserves the mode bits only when you pass -p. Without it, the destination inherits the umask-derived default.
cp -p deploy.sh /usr/local/bin/deploy.sh # preserves the +x bit
cp deploy.sh /usr/local/bin/deploy.sh # may land as 0644
Same story with rsync: the archive flag -a implies -p (permissions), so rsync -a keeps the bit. rsync -r alone does not. If you are deploying scripts over SSH, use -a and stop wondering why the bit keeps vanishing.
rsync -avz ./scripts/ user@host:/opt/scripts/
The third myth: sudo chmod 777 is the safe reset
Setting 777 on a script makes it world-writable, which on a shared Mac or a CI runner is a privilege-escalation hole, not a fix. The correct mode for a normal executable script is 755 (or 700 if only you should run it). Reserve 777 for nothing.
chmod 755 deploy.sh # rwxr-xr-x — the standard
chmod 700 deploy.sh # rwx------ — owner-only
If you inherited a script with 777, tighten it. On macOS, ls -l@ will also show any extended attributes (the @ in the listing above). Those are usually harmless — the quarantine attribute from a downloaded zip is the common one — but if the script came from the internet and macOS Gatekeeper is blocking execution, you will see a different message (“cannot be opened because the developer cannot be verified”), which chmod will not fix.
When chmod +x is not enough: shebang and line-ending traps
An executable script that still fails usually has a broken shebang line or Windows line endings, not a permission problem. The kernel reads the first line to find the interpreter; if it is wrong, you get bad interpreter or a confusing “permission denied” from the wrong binary.
Check the first line:
head -1 deploy.sh
# should print: #!/bin/bash or #!/usr/bin/env bash
Prefer #!/usr/bin/env bash so the script finds bash on $PATH instead of hard-coding a path. macOS ships /bin/bash as version 3.2 (frozen since 2007 due to GPLv3 licensing) — if your script uses associative arrays or mapfile, it needs bash 4+, which you must install separately (e.g. brew install bash) and then reference via env.
If the script was authored on Windows, strip CRLF line endings before doing anything else:
sed -i '' $'s/\r$//' deploy.sh
chmod +x deploy.sh
Note the '' argument — that is BSD sed syntax on macOS, not GNU sed. Omitting it is a common cross-platform mistake that produces sed: 1: "deploy.sh": invalid command code.
The fourth myth: the error is about file permissions, always
Not every “permission denied” comes from the file mode. On macOS, two other sources produce the same string and get misdiagnosed constantly.
1. The directory is not searchable. To execute a file, the kernel needs execute permission on every directory in the path. If /opt/scripts is drwx------ and owned by another user, ./deploy.sh fails with “permission denied” even though the script itself is 755.
namei -l /opt/scripts/deploy.sh # shows permissions on every path component
2. macOS TCC (Transparency, Consent, and Control). Since Catalina (10.15, 2019), macOS gates access to Documents, Desktop, Downloads, and removable volumes behind per-app consent. A script that reads or writes inside those folders from a terminal that has not been granted access will get “Operation not permitted” or “permission denied” — and no chmod will change that.
The fix is in System Settings → Privacy & Security → Full Disk Access (or Files and Folders), where you grant the terminal app itself access. This is the failure mode that eats the most time: people chmod the script, chmod the directory, run as root, and still fail, because TCC is enforced above the POSIX layer and does not care about root.
What to do next
Work through the checklist in order and stop at the first one that resolves it:
ls -l script.sh— is thexbit present? If not,chmod +x script.sh.namei -l $(realpath script.sh)— does every directory in the path havex?head -1 script.sh— is the shebang valid and pointing at an interpreter that exists?file script.sh— does it say “ASCII text” or “with CRLF line terminators”? Strip CRLF if the latter.- Is the path inside Desktop, Documents, Downloads, or an external volume? Check TCC in System Settings.
Then commit the fix. If the script lives in a git repo, run git update-index --chmod=+x script.sh so the execute bit is recorded in the index — otherwise every fresh clone will land with mode 100644 and the same error will greet the next developer. This is the failure mode to watch for: a local chmod +x that never makes it into version control, so the fix is invisible to everyone else and the bug comes back on the next clone.