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 “info.sh” script into “main.sh”, so all you have to do is add the following line into the “main.sh” script:

source info.sh

source-command-example

The info.sh script contains the following information: 

content-of-the-info.sh-script

Once you run the main.sh script, it will execute the info.sh first and then run the other commands: 

result-from-the-main.sh-script

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. 

dot-command-example

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

dot-slash-example

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:

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

For Medium Load:

#!/bin/bash
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:

#!/bin/bash
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/high_load.sh
else (( $(echo "$cpu_load > 1.0" | bc -l) ));
echo "Triggering moderate load script."
source scripts/moderate_load.sh
fi

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

executing-the-monitoring.sh-script

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