1. Overview

In Linux, we can source or execute a shell script. These two operations are pretty similar, yet different.

In this quick tutorial, we’ll discuss their differences through examples.

2. Both Sourcing and Executing Will Run Commands in the Script

First of all, both sourcing and executing a script will run all commands in the target script. A quick example may show it straightforwardly.

Let’s say we have a shell script myScript.sh:

$ cat myScript.sh
#!/bin/bash
echo "Hey, I'm a shell script file."

As the output above shows, the script is pretty simple. It contains only one single echo command.

Now, let’s execute and source this script:

$ ./myScript.sh
Hey, I'm a shell script file.

$ source myScript.sh
Hey, I'm a shell script file.

As we can see, both operations have executed the echo command in the target script.

Next, let’s see another example.

3. Extending the Script and Testing Again

First, let’s extend our myScript.sh file by adding a variable and a function:

$ cat myScript.sh
#!/bin/bash
echo "Hey, I'm a shell script file."

COUNTRY="United States of America"
echo "Now, the variable \$COUNTRY=$COUNTRY"

greeting() {
    echo "Hi $1, how are you doing?"
}

echo 'Say Hi to "Kent" by calling the function:' 
greeting "Kent"

Now, we have defined the COUNTRY variable and the greeting function in the script.

Next, let’s first source the script:

$ source myScript.sh
Hey, I'm a shell script file.
Now, the variable $COUNTRY=United States of America
Say Hi to "Kent" by calling the function:
Hi Kent, how are you doing?

The example above shows that we have the expected output.

After sourcing the file, let’s verify the variable value and test the greeting function:

$ echo $COUNTRY 
United States of America

$ greeting "Eric and Kevin"
Hi Eric and Kevin, how are you doing?

As the output shows, so far, so good, everything works as expected.

Next, let’s start a new shell and execute the myScript.sh script:

$ ./myScript.sh 
Hey, I'm a shell script file.
Now, the variable $COUNTRY=United States of America
Say Hi to "Kent" by calling the function:
Hi Kent, how are you doing?

Again, running the script produces the expected output. Next, let’s perform the same tests, verifying the variable and testing the function:

$ echo $COUNTRY

$ greeting "Eric and Kevin"
bash: greeting: command not found

This time, as we can see, even though it has printed the expected output as we execute the script, the echo $COUNTRY command prints nothing. Further, we’ve got the “command not found” error when we call the greeting function.

It seems the variable and the function are not recognized. So, let’s figure out why it happened.

4. Differences Between Sourcing and Executing a Script

When we source a script, the script is executed in the current shell. In other words, if we’ve declared new variables and functions in the script, after sourcing it, the variables and functions are valid in the current shell as well. That’s why our test has produced the expected result after we’ve sourced myScript.sh.

On the other hand, when we execute a script, it will be executed in a new shell, which is a subshell of the current shell. Therefore, all new variables and functions created by the script will only live in the subshell. After the script is done, the subshell process is terminated, too. Thus, the changes are gone.

So, in our previous test, after executing myScript.sh, the $COUNTRY variable and the greeting function are not available in the current shell.

5. Conclusion

In this quick article, we’ve discussed the difference between sourcing a script and executing a script in Linux through examples.

In short:

  • Sourcing a script runs the commands in the current shell process. Changes to the environment take effect in the current shell, too.
  • Executing a script runs the commands in a subshell process. Changes to the environment only take effect in the subshell and are lost when the script is executed.
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.