More Bash answers

by Danny
Published 3 min read

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.


_10
chmod +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 see bad interpreter: No such file or directory instead, but a shebang pointing at a file that is not executable gives Permission denied.
  • A noexec mount. Some systems mount /tmp, home directories on shared machines, USB drives or container volumes with the noexec option. Nothing on that filesystem can be executed directly, no matter what chmod says.

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
_10
chmod +x script.sh
_10
_10
# Or only for the owner
_10
chmod 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:


_10
git update-index --chmod=+x script.sh
_10
git 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
_10
echo "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:


_10
file script.sh # reports "with CRLF line terminators" if affected
_10
sed -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:


_10
findmnt -T script.sh -o TARGET,OPTIONS
_10
# or, more generally
_10
mount | 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:


_10
bash 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 +x on a copy, then executing the original. Check with ls -l that the x appears on the file you are actually running.
  • Forgetting the ./. Typing script.sh on its own makes the shell search your PATH, so you get command not found rather than running the file in the current directory.
  • Expecting chmod to work on a noexec mount or a Windows drive mounted in WSL with restrictive options. The permission change may be silently ignored.
  • A Permission denied on 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.
#

Was this resource helpful?