How to Fix ‘bash: ./script.sh: Permission denied’ in Linux

Attempting to execute an automation script, software deployment installer, or custom utility in the Linux command line by running ./script.sh only to be blocked with the error message: “bash: ./script.sh: Permission denied” or “-bash: ./setup.sh: Permission denied” halts administration workflows immediately. In Linux security architecture, the kernel verifies discretionary access control (DAC) permissions before attempting to load an executable into memory. Unlike Windows (which determines executability based on file extensions like .exe or .bat), Linux completely ignores file extensions and strictly requires the POSIX executable bit (x) to be set on the file’s mode. Furthermore, filesystem mount options (such as noexec) or mandatory access controls (SELinux / AppArmor) will prevent binary execution even if you are logged in as the root superuser. This guide explains why this permission barrier occurs and provides verified methods to fix it.

What Causes ‘Permission denied’ When Executing Scripts?

The Linux kernel rejects script execution under several permission and filesystem conditions:

  • Missing Executable Permissions (Mode Bits): The file was newly created or downloaded via git, curl, or text editors with default read/write permissions (e.g., 0644 / -rw-r--r--) lacking the execute bit (+x).
  • Filesystem Mounted with the noexec Flag: The script resides on a partition (frequently /tmp, /var/tmp, removable USB drives, or NFS shares) mounted with the noexec security mount option.
  • Wrong File Ownership and Group Attributes: The script is owned by another user and lacks read/execute permissions for your active user account or group.
  • Malformed or Missing Shebang Line: An invalid interpreter path specified in the file header (e.g., pointing to a non-existent #!/bin/bash^M with Windows CRLF carriage returns).
  • SELinux or AppArmor Security Context Blocks: Security policies restricting scripts located outside standard executable directories from calling system binaries.

Method 1: Grant Executable Permissions via chmod +x

The standard resolution for newly created or downloaded scripts is granting execute rights using the chmod command.

  1. Open your Linux terminal and navigate to the directory containing your script:
    cd /path/to/directory
  2. Inspect the current permissions and ownership of the script using ls -l:
    ls -l script.sh
    If the permissions display -rw-r--r--, the file lacks executable flags.
  3. Grant executable permissions to the current user (or all users):
    # Grant execute to all users:
    chmod +x script.sh
    
    # Or grant execute strictly to the file owner:
    chmod u+x script.sh
  4. Verify the updated permissions: the output should now display -rwxr-xr-x and the filename may appear in green text.
  5. Execute the script normally:
    ./script.sh

Method 2: Execute Directly via the Shell Interpreter

If you cannot or do not wish to modify file permissions, passing the script directly as an argument to the Bash interpreter bypasses the execute bit requirement.

  1. In your terminal, invoke bash or sh directly followed by the script filename:
    bash script.sh
    # Or for POSIX standard shells:
    sh script.sh
  2. How this works: The Bash binary itself (/bin/bash) already has the executable bit enabled. When invoked this way, Bash merely needs read (r) permission on script.sh to parse its commands line by line.
  3. If the script requires superuser privileges, run:
    sudo bash script.sh

Method 3: Fix Filesystem ‘noexec’ Mount Restrictions

In enterprise Linux environments, system administrators frequently mount temporary directories (like /tmp) with the noexec flag to prevent malicious payload execution.

  1. Check if the filesystem hosting your script is mounted with noexec:
    mount | grep " $(df -P . | tail -1 | awk '{print $6}') "
  2. Inspect the mount options inside the parentheses. If you see noexec, the kernel will refuse to execute any binary or script on this partition regardless of chmod +x or root privileges.
  3. Remediation Options:
    • Move the script: Move your script to an executable partition (such as your user home directory ~/scripts or /opt):
      mv /tmp/script.sh ~/script.sh && cd ~ && ./script.sh
    • Temporarily remount the volume with execution enabled:
      sudo mount -o remount,exec /tmp

Method 4: Convert Windows Line Endings (CRLF to LF)

Scripts authored on Windows systems contain carriage return characters ( or ^M) that corrupt the shebang line.

  1. Check if your script contains Windows-style CRLF line endings:
    head -n 1 script.sh | cat -A
    If you see #!/bin/bash^M$, the kernel attempts to invoke an interpreter named bash , which fails with permission or not found errors.
  2. Install and run the dos2unix conversion utility:
    sudo apt install dos2unix    # Debian / Ubuntu
    sudo dnf install dos2unix    # RHEL / Fedora
    dos2unix script.sh
  3. Alternatively, strip carriage returns using sed:
    sed -i 's/
    $//' script.sh
  4. Test running ./script.sh again.

Method 5: Check File Ownership and SELinux Contexts

If the file is owned by another user or blocked by mandatory access controls:

  1. Verify that your user account owns the file:
    sudo chown $USER:$USER script.sh
  2. On RHEL, AlmaLinux, or Fedora systems running SELinux, check the security context:
    ls -Z script.sh
  3. If the context is mislabeled, restore the default target context:
    restorecon -v script.sh

Script Permission Denied Troubleshooting Matrix

Cause Diagnostic Command Primary Fix
Missing execute bit ls -l shows -rw-r–r– chmod +x script.sh
noexec mount option mount | grep noexec Move script to home directory (~/)
Windows line breaks cat -A shows ^M dos2unix script.sh
Script execution locked Direct execution forbidden Run explicitly with: bash script.sh

Applying the executable bit and ensuring the script resides on an executable filesystem permanently resolves permission denied errors in the Linux terminal.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.