Solving `permission denied` error for shell scripts

If try to run a script, whether it is written in Python, bash or something else, you may encounter an error saying Permission denied.

Lets say for example you have a file called script.sh which runs perfectly fine when you run it using a interpreter like:

$ bash ./script.sh
Hello World

However, you prefer to run it without the bash and use just the file name. Or later even as a command instead by adding it to your PATH.

In that case you may see the following error.

$ ./script.sh
bash: ./script.sh: Permission denied

This error indicates that the execute file permission is missing. In order to add this execute permission you use the chmod command, which is short for change file mode. By running the following command we can make the script executable.

$ chmod u+x ./script.sh

And as we now see this indeed did the trick. Make sure you have added the shebang comment though.

$ ./script.sh
Hello World

See Also

6 Non-Programming Books for Programmers