BASH Programming

What Is the Difference between Sourcing and Executing a Shell Script

In Unix-like operating systems, a shell script is a file containing a series of commands that are interpreted and executed by the shell program. When it comes to running a shell script, there are two main ways to do it: sourcing and executing. While they may seem similar, they have different effects on the current shell session and environment variables, this article will elaborate the differences between the two methods.

What Is the Difference between Sourcing and Executing a Shell Script

To understand the difference between both the ways one must first have a sound knowledge for using both the methods so first let’s have their basic definition one by one:

Sourcing a Shell Script

Sourcing a shell script is a way of running a script in the current shell environment. All commands of a script are executed if they are typed directly into the command line interface and sourcing a script does the same thing. Because of this it provides an option to the script to modify the environment variables and set shell options in the current shell.

For further illustration I have made a test file for bash script and then executed it, here is the code for the bash file I created:

# bashfile.sh

export my_info="Hello linuxhint"

Here I have created an environmental variable that is my_info which has Hello Linuxhint stored in it and then executed it by using the given syntax:

. <bash-file-name>

Since I have created a file named bashfile.sh so I used the above syntax like this:

. bashfile.sh

Once the file is executed, I have called the environmental variable to see what is stored on it and it returns value stored in it which clearly shows that changes to the environment variables persist in the current shell:

Executing a Shell Script

Executing a shell script is a way of running a script in a separate shell. When a script is executed, a new shell is created, the script is run in that shell, and the shell terminates when the script is finished. This shows that any changes made to the environment variables or shell options in the script will not persist after the script is finished. For further illustration I have made a test file for bash script and then executed it, here is the code for the bash file I created:

# bashfile.sh

export my_info="Hello linuxhint"

Here I have just created an environmental variable that is my_info which has hello Linuxhint stored in it and then executed it by using the given syntax:

./<bash-file-name>

Since I have created a file named bashfile.sh so I used the above syntax like this:

./bashfile.sh

Once the file is executed, I have called the environment variable to see what is stored on it and it returns a blank which clearly shows that changes made to the environment variables or shell options in the script will not persist after the script is finished:

To further illustrate the difference there the table given below that pinpoint some of the key differences between sourcing and executing a file:

Factors Executing a shell script Sourcing a shell script
Aliases and Functions Aliases and functions only persist in the script’s shell. Aliases and functions persist in the current shell.
Environment Changes to environment variables only persist in the script’s shell. Changes to environment variables persist in the current shell.
Syntax ./script-name.sh or bash script-name.sh . script-name.sh or source script-name.sh
Main utility Run a self-contained script without affecting the current shell. Set up the environment variables or aliases for the shell

Conclusion

There are two ways to run the bash script one is by sourcing it and the other is by executing it both the ways have their own significance. The Main difference between the two is that sourcing the script file keeps the changes to the environment variables in the current shell, whereas executing the script will keep the changes.a

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.