Why running a shell script gives Permission denied, and how to fix it with chmod +x, a correct shebang line, or by working around a noexec mount.
The short answer
#Your script file is missing the execute permission. Unix-like systems will only run a file directly (as ./script.sh) if it is marked executable for your user. Add the permission with chmod and run it again.
_10chmod +x script.sh_10./script.sh
If that still fails, check the first line of the script (the shebang) and whether the file lives on a filesystem mounted with noexec.
Why it happens
#When you type ./script.sh, you are asking the kernel to execute the file. Before it does, it checks the file's permission bits. Every file has read, write and execute bits for three groups: the owner, the group, and everyone else. You can see them with ls -l:
_10$ ls -l script.sh_10-rw-r--r-- 1 dan staff 120 27 Nov 10:12 script.sh
That -rw-r--r-- means the owner can read and write, everyone else can read, and nobody can execute. New files created by editors, touch, git clone on some setups, or downloads usually start without the execute bit, so the kernel refuses with Permission denied.
There are two other common causes that produce the same message or a close cousin:
- A broken shebang. The first line, such as
#!/usr/bin/env bash, tells the kernel which interpreter to run. If it points at a path that does not exist you usually seebad interpreter: No such file or directoryinstead, but a shebang pointing at a file that is not executable givesPermission denied. - A
noexecmount. Some systems mount/tmp, home directories on shared machines, USB drives or container volumes with thenoexecoption. Nothing on that filesystem can be executed directly, no matter whatchmodsays.
How to fix it
#Add the execute bit
This is the fix nine times out of ten.
_10# Executable for everyone who can read it_10chmod +x script.sh_10_10# Or only for the owner_10chmod u+x script.sh
If the script is in a Git repository, record the permission in Git so it survives a fresh clone for your teammates and CI:
_10git update-index --chmod=+x script.sh_10git commit -m "Make script.sh executable"
Check the shebang line
Make sure the very first line of the file is a valid shebang, with no blank line or spaces before it:
_10#!/usr/bin/env bash_10echo "Hello from Bash"
#!/usr/bin/env bash finds bash on your PATH, which is more portable than hard-coding /bin/bash. If the file was edited on Windows, it may have CRLF line endings, which make the interpreter path end in an invisible carriage return. Check and fix that with:
_10file script.sh # reports "with CRLF line terminators" if affected_10sed -i 's/\r$//' script.sh # GNU sed; on macOS use: sed -i '' 's/\r$//' script.sh
Work around a noexec mount
First confirm it is the problem. findmnt is available on most Linux systems:
_10findmnt -T script.sh -o TARGET,OPTIONS_10# or, more generally_10mount | grep noexec
If the options include noexec, either move the script somewhere that allows execution, or pass it to the interpreter yourself. Running bash script.sh reads the file as input instead of executing it, so it does not need the execute bit:
_10bash script.sh
Do not remount system filesystems without noexec just to run a script. That option is usually there on purpose.
Running it with sudo does not help
sudo ./script.sh still needs the execute bit, and running scripts as root to dodge a permission error is a habit worth avoiding. Fix the permission instead.
Common pitfalls
#- Running
chmod +xon a copy, then executing the original. Check withls -lthat thexappears on the file you are actually running. - Forgetting the
./. Typingscript.shon its own makes the shell search yourPATH, so you getcommand not foundrather than running the file in the current directory. - Expecting
chmodto work on anoexecmount or a Windows drive mounted in WSL with restrictive options. The permission change may be silently ignored. - A
Permission deniedon a directory in the path. You need execute permission on every directory leading to the script to reach it. - Committing a script without the executable bit, so it works on your machine and fails in CI. Use
git update-index --chmod=+x.
Related reading
#Was this resource helpful?