How to Fix Shell Script Permission Denied Error in Linux

Published on January 19, 2024

As a Linux user, shell scripts will inevitably become part of your life. From running programs to automating tasks, shell scripts offer numerous applications. When you create your shell script, you may run into the “permission denied” error, which represents itself as bash:/ program-name: permission denied.

Before you get frustrated about this, there is a way you can fix the error and get your script to run seamlessly. Today’s guide focuses on understanding what this error means and gives solutions for fixing the problem under different circumstances. Read on!

What Does the Shell Script Permission Denied Error in Linux Mean

One benefit of running Linux is using shell scripts to simplify your life. Whether it’s the basic repetitive tasks such as updating the system or creating scheduled backups, shell scripts offer a convenient way of doing this. By writing your script, you can quickly run it with a single command and execute all the tasks defined in it.

Assume you have the update.sh script below. To run it, simply use the ./script-name syntax, and your file will execute. However, you may get the permission denied error, as in the case below.

A script is a Linux file. Therefore, it comes with permissions which limit your access and modification. By default, when you create a shell script, it isn’t executable until you give it the execute permission for all users or specific user categories.

How to Fix Shell Script Permission Denied Error in Linux

To quickly resolve the problem, start by checking the current file permissions for the script. For that, run the command below.

Copy

The output below shows that the user, group, and others don’t have the execute (x) permission. Therefore, whenever you try to execute the script, you will get the “permission denied” error. A quick way to know if the script is executable is to check the color of its name. Notice that it is white. If it were executable, its color would be green.

Now that we know what is causing the error, the next step is to fix it. The Linux chmod command allows users to add or remove permissions to a file. For our case, we want to add the execute permission. Let’s quickly understand the permission structure in the file.

Copy

The permissions are divided into three sections representing the user (u), group (g), and others (o). The r stands for read, the w stands for write, and the x stands for execute. Returning to the current permissions in our shell script, the permissions are as follows: u=rw-, g=r–, and o=r–.

Suppose we want to make the script executable by everyone. We will add the x permission as follows.

Copy

Rerun the ls -l command to check the new file permissions. Notice how each category now has the execute (x) permission, and our script is now in green, confirming it’s executable.

Sometimes, adding the execute command for everyone is not recommended, especially if the script has administrative tasks. Such a case requires you to specify what permission to add to make it executable and for which category. Let’s say we only want the user category to have the execute permission. For that, we would run the chmod command as follows:

Copy

Rechecking the file permissions shows that our user now has the rwx permissions, allowing them to read, write, and execute the shell script.

Having changed the file permissions for the shell script, we can now execute it and verify that we managed to fix the permission denied error. Run the script as shown below.

Copy

That’s it! We’ve fixed the shell script permission denied error in Linux, and our script executes as expected.

Conclusion

When working with a shell script, creating it doesn’t automatically make it executable. Unless you have the correct file permissions for that script, you will run into the permission denied error. In most cases, adding the execute command is the quickest way to fix the error. However, you should start by checking the script’s permissions to know what permissions are missing before adding them. The example covered in this post has demonstrated what it takes to fix shell script permission denied error in Linux.

New to LinuxMeta? Get Started Now! 

Instantly Deploy Linux & Windows KVM VPS at a Cheap Price