Fixing 'bash: ./script.sh: Permission denied' When Running Bash Scripts

beginner๐Ÿง Linux2026-03-16| Commonly encountered on Linux distributions (like Ubuntu, CentOS, Fedora, Debian) and other Unix-like operating systems.

Error Message

bash: ./script.sh: Permission denied
#bash#chmod#permission#linux#scripting#troubleshooting

The Problem: 'bash: ./script.sh: Permission denied'

You've poured effort into writing a helpful Bash script, saved it, and now you're eager to run it. But instead of your script executing, your terminal throws back a frustrating message:

bash: ./script.sh: Permission denied

This error is incredibly common, especially for those new to Linux or when moving scripts between different systems. Essentially, it means your operating system is blocking the script from running because it lacks the necessary permissions.

TL;DR: The Quick Fix

Most often, this error pops up because your script doesn't have the crucial execute permission. Here's the fastest way to get things working:

  • Grant execute permission:
chmod +x script.sh

  • Run your script:
./script.sh

If that did the trick, fantastic! You're all set. If not, don't worry; keep reading for a more in-depth explanation and other potential solutions.

Understanding 'Permission denied'

In Linux and similar Unix-like operating systems, file permissions are a core part of their security model. Each file and directory has specific permissions that dictate who can read, write, or execute it. When you see 'Permission denied' for a script, it specifically tells you that the user trying to run it doesn't have the critical execute permission for that file.

When you try to run a script using ./script.sh, you're directly instructing your shell to execute it. The operating system then checks if the file has the 'execute' bit enabled for your user or group. If that bit is missing, the 'Permission denied' error appears.

This situation frequently arises for a couple of reasons:

  • You created the script yourself. By default, many systems don't automatically assign execute permissions to newly created files.
  • You downloaded or copied the script from an external source, like a Windows system or a website. In these cases, file permissions might not transfer correctly.

Step-by-Step Solutions

Method 1: Add Execute Permissions with chmod

This is often the simplest and most direct fix. The chmod command (short for 'change mode') is your go-to tool for modifying file permissions.

1. Check Current Permissions

Before making any changes, it's wise to inspect your script's current permissions using ls -l:

ls -l script.sh

You'll see output similar to this example:

-rw-r--r-- 1 user user 1234 Mar 16 10:00 script.sh

The crucial part to examine is -rw-r--r--. Let's break down what these characters mean:

  • The first character (-) indicates that it's a regular file (d would mean directory).
  • rw-: The file's owner (user) can read and write, but lacks execute permission.
  • r--: Members of the owning group (also user in this example) can read, but can't write or execute.
  • r--: All other users can read, but also can't write or execute.

Notice the absence of an 'x' (execute) for the owner. That's almost certainly why you're encountering the error.

2. Grant Execute Permission

To add execute permission specifically for the file's owner (which is usually you), use this command:

chmod +x script.sh

The +x option adds the execute permission. If you need to grant execute permission to everyone (owner, group, and others), you could use chmod a+x script.sh or the octal mode chmod 755 script.sh. For instance, 755 grants read/write/execute to the owner, and read/execute to the group and others.

3. Verify Permissions

Run ls -l again to confirm your change:

ls -l script.sh

You should now see something like this:

-rwxr--r-- 1 user user 1234 Mar 16 10:00 script.sh

The 'x' character after 'rw' now confirms that the owner has execute permission.

4. Run the Script

With the execute permissions correctly set, you can now run your script without issues:

./script.sh

Method 2: Explicitly Run with an Interpreter

Even if a script lacks execute permissions, you can often still run it by directly telling your shell which interpreter to use. This method works because you're executing the interpreter (e.g., bash) itself, and then passing your script as an argument to it. The interpreter already has execute permissions, allowing it to read and process your script's contents.

1. Using bash

bash script.sh

This command instructs your system to use the bash interpreter to run script.sh. In this scenario, the script file itself doesn't need execute permissions, as bash is handling the execution.

2. Using sh (for POSIX-compliant scripts)

sh script.sh

Similar to using bash, this approach employs the default shell (often linked to bash or dash) to execute the script.

While this method effectively gets your script running, it's generally best practice to grant execute permissions if you intend for the script to be run directly using ./script.sh.

Method 3: Correct File Ownership with chown

This scenario is less common for a basic 'permission denied' error. However, it's highly relevant if the script's current owner isn't the user attempting to run it, and that user doesn't have sufficient group or 'other' permissions. The chown command (change owner) allows you to modify a file's owner and/or group.

1. Check Owner and Group

Use ls -l script.sh again. Pay close attention to the third and fourth columns, which display the owner and group, respectively:

-rwxr--r-- 1 **otheruser** **othergroup** 1234 Mar 16 10:00 script.sh

2. Change Ownership

If you're logged in as youruser but the script is owned by otheruser, you'll likely need to change its ownership. This action typically requires sudo privileges.

sudo chown youruser:yourgroup script.sh

Remember to replace youruser and yourgroup with your actual username and primary group. After changing ownership, ensure that the execute permissions are properly set, as explained in Method 1.

Method 4: Check Parent Directory Permissions

Occasionally, the problem isn't with the script itself, but with the directory where it's located. If you lack the necessary permissions to list or access that directory, you won't be able to run any scripts contained within it.

1. Check Directory Permissions

Inspect the permissions of the directory containing your script:

ls -ld .

The 'd' at the beginning of the permission string confirms it's a directory. For directories, 'execute' permission is essential; it allows you to cd into the directory and access its contents.

2. Grant Directory Permissions (if needed)

If the directory permissions are too strict (e.g., ---r--r-- for 'others' or your specific group), you might need to add read and execute permissions:

chmod o+rx .

Or, if you need to grant permissions to a specific group:

chmod g+rx .

Always exercise caution when altering directory permissions, especially in shared environments, as it can affect other users.

Verification: Confirming the Fix

After applying any of these solutions, particularly Method 1, always take a moment to verify your changes:

  • Re-check file permissions:
ls -l script.sh

Confirm that the 'x' (execute) bit is now present for the correct user or group.

  • Run the script again:
./script.sh

If your script executes successfully and performs its intended task without the 'Permission denied' error, then your fix is confirmed!

Further Reading and Resources

  • man chmod: Dive deeper into the details of changing file permissions.
  • man chown: Learn more about modifying file ownership.
  • A comprehensive guide to Linux File Permissions: Understand the 'rwx' system and octal modes like 755 and 644.

Related Error Notes