BASH Programming

Bash Script Include Another Bash Script

If you are a Linux enthusiast, then you have probably heard of the bash scripting concept. In bash scripting, you can automate the code block of the command sequence. However, sometimes, the code block in bash scripting becomes complex, and it can be tough for a user to execute it further.

That’s why bash offers a feature that includes a script in another. It lets you run the commands efficiently and achieve an enhanced level of automation. In this blog, we will explain the concept and provide examples of how to include a bash script into another script.

The source Command

The source is one of the most common commands to include a bash script into another. For example, if you want to add the “” script into “”, so all you have to do is add the following line into the “” script:



The script contains the following information:

Once you run the script, it will execute the first and then run the other commands:

Dot (.) or Dot-Slash 

You can use dot (.) instead of source command as both are similar and used to execute the command from the current shell. 


You can also use the dot-slash (./) instead of dot (.) to execute the command from the current directory:


How to Include Multiple Scripts Into One Script

Let’s create a script to check the current CPU load and execute the other script according to the load. Now, we need to create two scripts, one for the high load and the other is for the medium load:

For High Load:

echo "High CPU load detected."
echo "Killing non-essential processes..."
pkill -9 -f firefox
echo "Non-essential processes have been terminated."

For Medium Load:

echo "Moderate CPU load."
echo "Logging current process statuses..."
ps -aux > /tmp/current_processes.txt
echo "Process log created at /tmp/current_processes.txt."

The script above creates a tmp text file if the system has a medium CPU load. Let’s now create a third script, which will have the above two scripts link:

cpu_load=$(uptime | awk -F '[a-z]:' '{ print $2}' | cut -d, -f2 | xargs)
if (( $(echo "$cpu_load > 2.0" | bc -l) )); then
echo "Triggering high load script."
source scripts/
else (( $(echo "$cpu_load > 1.0" | bc -l) ));
echo "Triggering moderate load script."
source scripts/

Finally, save and run the script by running the below command in the terminal:

Wrapping Up

So, this was all about the simple ways to include one script to another and enhance the automation with no hassles. We have also included an example showing how you can perfectly include multiple scripts to break the complex processes without catering to the automation. We recommend you explore creativity and create various scripts around linking scripts. 

About the author

Prateek Jangid