While executing programs in Unix, a common problem which is encountered is the bash: ./program_name: permission denied error. The error usually occurs when the script you are trying to execute does not have the necessary permissions. Modifying the permissions of the script or assigning the correct ones will fix the error.
Let us look into the details of the error and also its solution.
What do you mean by bash: ./program_name: permission denied error?
The permission denied error is encountered when the script you are running does not have the execute permission. Unix and similar operating systems usually not execute a shell script if it does not have the permission to execute.
Look at the following example -
# /opt/myscript.sh
bash: /opt/myscript.sh: Permission denied
In this program, the error is raised as the script “myscript.sh” does not have execute permission.
To check the permissions assigned to a file, type in the following command in the command line –l
s -l myscript.sh
# ls -l myscript.sh
-rw-r--r-- 1 root root 6 Jun 17 06:40 myscript.sh
$getfacl myscript.sh
# file: m.sh
# owner: root
# group: root
user::rw-
group::r--
other::r--
In the above result, you can see that the script only has read and write permissions, but no execute permissions (denoted by x). Hence, the file cannot be executed.
Solution to fix the bash: ./program_name: permission denied error
The only way to fix the error is to change the file permission settings of the script. You can do this using the chmod command, which stands for change mode. Look at the two commands –
- chmod u+x program_name– In this line, the chmod command will change the access mode to execute, denoted by x. only the file’s owner will have the permission to execute the file.
- sudo chmod +x program_name– Here, the chmod command will provide the execute permission to everyone as no reference is specified.
Chmod references include:
- u – The file owner
- g – Users who belong to the file’s group
- o – Users who are neither owners nor members of the file’s group
- a – Every user
Solution Code
chmod +x myscript.sh
# ls -l myscript.sh
-rwxr-xr-x 1 root root 6 Jun 17 06:40 myscript.sh
Here, the chmod command assigns execute permission to every user, as no specific reference is mentioned. After running the script you can see that execute permission was assigned to the file.
You can check this by running the getfacl command. The getfacl command is used for displaying the file name, owner’s name, file group and the Access Control List (ACL). The default ACL will also be displayed.
$ getfacl myscript.sh
# file: myscript.sh
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
After running this script, this is the final result –