1. Overview

If a shell script isn’t executable, we cannot run it directly in the terminal. Of course, the execute permission of the script can be changed so that we can run it. But, that may not always be possible. For example, if the script belongs to another user, we may not have the necessary privileges to change the execute permission of the script using the chmod command.

In this tutorial, we’ll discuss running a script that isn’t executable.

2. Analyzing the Problem

If we have an executable script, we can run it directly in the terminal. Suppose we have the following script, hello_world.sh:

#!/bin/bash
echo “Hello World"

This script just prints Hello World and exits. We specify the interpreter of the script on the first line after the shebang (#!). In our case, the interpreter is /bin/bash. This tells the operating system to use Bash as the interpreter for parsing the remainder of the script.

Let’s first check the execute permission of this script:

$ ls –l hello_world.sh
-rwxr-xr-x 1 alice alice 31 May  6 08:32 hello_world.sh

Since the execute permission of the script is set for all users, we can run it directly in the terminal:

$ ./hello_world.sh
Hello World

Now, we’ll consider the case when the script isn’t executable. First, let’s remove the execute permission of the script hello_world.sh using chmod:

$ chmod -x hello_world.sh
$ ls –l hello_world.sh
-rw-r--r-- 1 alice alice 31 May  6 08:32 hello_world.sh

As it’s apparent from the output of the ls –l command, nobody – not even the owner of the script – has the right to execute this script. Let’s try to run it:

$ ./hello_world.sh
-bash: ./hello_world.sh: Permission denied

Our objective is to run the script hello_world.sh in the remainder of this tutorial.

3. Specifying the Shell Explicitly

One way to run a script that we don’t have the execute permission is to specify the shell explicitly while running the script. Let’s run the script, hello_world.sh, with the Bash interpreter:

$ /bin/bash ./hello_world.sh
Hello World

As we see, although the execute permission of the script isn’t set, we were able to run it. However, it’s a little less convenient to run it since we have to specify the interpreter on the command line. Here, we explicitly specified the interpreter /bin/bash, which executes the script.

If we’re able to specify the interpreter while running the script, we can run the script with another interpreter, for example, the Korn shell:

$ /bin/ksh ./hello_world.sh
Hello World

In this case, the interpreter we specify while running the script, /bin/ksh, overrides the one specified in the script after the shebang.

For this simple script, running the script with both Bash and Korn shells was successful, but that may not be always the case. The syntax of different shells may not be fully compatible with each other.

4. Sourcing the Script

Another way to run a script that isn’t executable is using the source command. Let’s run the script, hello_world.sh, using source

$ source ./hello_world.sh
Hello World

As we see, we ran a script that isn’t executable by sourcing it.

Sourcing a script means that we run the script in the current shell. So, if the script contains an exit command, we must be cautious since we may exit from the current shell itself.

We can use the dot or period character (.) instead of source and get the same result:

$ . ./hello_world.sh
Hello World

5. Conclusion

In this article, we discussed two different ways to execute a shell script that isn’t executable.

First, we showed that we can run a script that doesn’t have execute permission by specifying the shell explicitly while running it.

As an alternative to the first method, we saw that we could run a script that isn’t executable using the source command.

Comments are closed on this article!