Skip to content

/errors/bash/permission-denied

bash: ./script.sh: Permission denied

verified onenvironmentlocal · linux 6.18.44-fc-v140 x64 · GNU bash 5.2.21updated
tool
Bash
exit code
126
versions
bash 4.x, bash 5.x
os
linux, macos

bash: ./script.sh: Permission denied with exit status 126 means bash found the file but the kernel refused to execute it. Most often the file lacks the execute bit. Run chmod +x ./script.sh, or start it with bash ./script.sh.

Reproduce it

bash
printf 'echo hi\n' > run.sh
./run.sh

Output · exit 126

bash: line 2: ./run.sh: Permission denied

Exit status 126 means "found but not executable"; 127 means "not found" (see command not found).

Fix it

bash
printf 'echo hi\n' > run.sh
ls -l run.sh | cut -c1-10
chmod +x run.sh
ls -l run.sh | cut -c1-10
./run.sh

Output

-rw-r--r--
-rwxr-xr-x
hi
CheckCommand
Permission bits and ownerls -l script.sh
Mount options (look for noexec)findmnt -T script.sh
Run without the execute bitbash script.sh

A script started with bash script.sh ignores its permission bits and its shebang. If Git loses the bit, record it with git update-index --chmod=+x script.sh.

Other places the message appears

The same words show up in situations that are not about the script's own mode. Read the part of the message before Permission denied to tell them apart:

  • bash: ./script.sh: Permission denied with exit 126: the file cannot be executed, as on this page.
  • bash: out.log: Permission denied after a redirection such as > out.log: you cannot write that file or create files in that directory. Check the directory with ls -ld .; chmod +x does not help here.
  • A program started from the script reports Permission denied for a file it opens: that is the program's own error about the file it tried to read or write, and its exit status is usually 1.

Platform notes

In containers, bind-mounted files keep the host's permission bits, so a script that is executable inside the image can lose its bit when the volume replaces it. On Windows, Git Bash decides executability from the file's content and extension instead of Unix mode bits, so the error looks different there and chmod has little effect.

Prevention

Keep scripts under version control with their executable mode recorded, start them in CI with an explicit bash script.sh when the checkout may not preserve modes, and avoid running scripts from /tmp on hardened systems where it is mounted noexec.

FAQ

Why does sudo ./script.sh still say Permission denied?

sudo changes the user, not the file's mode. If the execute bit is missing or the file system is mounted noexec, root is refused too; add the bit with chmod +x or run sudo bash ./script.sh.

Is chmod 777 a good fix?

No. It lets every user on the system modify the script, which is a security risk. chmod u+x or chmod 755 is enough to run it.

What is the difference between exit status 126 and 127?

126 means the file was found but could not be executed, usually this permission problem. 127 means bash did not find the command at all.

Sources