bash permission denied — how to fix (chmod +x)
Quick Answer
# Add execute permission for the owner
chmod +x script.sh
# Run it
./script.sh
# Or run without changing permissions
bash script.sh
When this happens
bash: ./script.sh: Permission denied
# or
zsh: permission denied: ./script.sh
The file does not have the execute bit set. You can either add it with chmod, or run the script by passing it to bash explicitly.
Other causes & fixes
Check current permissions
ls -la script.sh
# -rw-r--r-- 1 user group 512 Jan 1 12:00 script.sh
# ^^^ no x — not executable
# After chmod +x:
# -rwxr-xr-x 1 user group 512 Jan 1 12:00 script.sh
chmod permission modes
chmod +x script.sh # add execute for all
chmod u+x script.sh # add execute for owner only
chmod 755 script.sh # rwxr-xr-x (owner:rwx, others:r-x)
chmod 700 script.sh # rwx------ (owner only)
File on a noexec-mounted filesystem
If the file lives on a filesystem mounted with noexec (common for /tmp or NFS), chmod +x will not help.
# Check mount options
mount | grep noexec
# Copy the script to a normal filesystem first
cp /tmp/script.sh ~/script.sh && chmod +x ~/script.sh && ~/script.sh
Related