fix permission denied error in linux

How to Fix Permission Denied Error While Running Shell Script

Often when you try to run a shell script you may get “Permission Denied” error. This is a very common issue, especially if you have newly created a shell script, or copied it from someplace else, or extracted it from an archive such as .tar.gz file. In this article, we will look at how to fix permission denied error while running shell script.


What is Permission Denied Error in Linux?

When you try to run a shell script in Linux, you may get an error saying “Permission Denied”. This is because the user you are logged in as does not have sufficient permission to run shell script. In many cases, only users with sudo or root privileges have the permission to execute shell script. Here is an example.

$ ./test.sh
bash:./test.sh:Permission Denied


How to Fix Permission Denied Error While Running Shell Script

Here is how to fix permission denied error while running shell script.


1. Check File Permissions

Open terminal and run the ls -l command to list file permissions.

$ ls -l ./test.sh

In the output, the 9 characters starting from 2nd to 11th indicate user permissions for this file.


2. Make Shell Script Executable

Run the following command to make file executable by everyone. We use chmod command to update file permissions of shell script. In this command, we issue +x option add executable privileges for all.

$ sudo chmod +x ./test.sh


3. Check Shell Script Execution

Try running the shell script with the following command. It should run this time.

$ ./test.sh

In this article, we have seen how to fix ‘permission denied’ issue when you run shell script.

Also read:

How to Get Filename from Path in Shell Script
Shell Script to Trim Whitespace
How to Disable HTTP TRACE Method in Apache
How to Switch User in Ubuntu Linux
How to Bring Background Process to Foreground

Leave a Reply

Your email address will not be published. Required fields are marked *