How to Call Another Script in Bash [2 Methods]

Calling another script within a Bash script is an important task. Once you call another script, the execution of the current script will temporarily pause and the code of the called script will be executed first.

It offers the user to write code in different scripts and facilitates modularity and reusability of the code. Moreover, scriptwriters can break down complex code into different and more manageable scripts. This article deals with various ways to call another script in Bash.

Key Takeaways

  • Calling another script from the running script.
  • Accessing variable and function of the calling script.
  • Reasons for calling another function may not work.

Free Downloads

2 Methods to Call Another Script in Bash Script

There are mainly two ways of calling another script. One can call a script in the same process as the running script or can call the script in a different process. The two methods have some inherent differences that you can explore later in this article.

You can read the Comparative Analysis of Methods to find the easy one for you.

Method 1: Call and Run Another Script in the Same Process

One can call another process in the running script using the source command or its equivalent dot(.) operator. In this way, the calling method is executed in the running script’s process and the variables and functions of another script are accessible.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file named same_process.sh in the build-in nano editor:

nano same_process.sh
EXPLANATION
  • nano: Opens a file in the Nano text editor.
  • same_process.sh: Name of the file.
Creating .sh file❸ Copy the following scripts and paste them into nano. Press CTRL+O and ENTER to save the file; CTRL+X to exit. Alternatively, copy the following script. Paste the script in a text editor and save it as .sh file.

Script (same_process.sh) >

#!/bin/bash

# Prompt the user to enter a number
read -p "Please enter a number: " number

# Execute another script and pass the number as an argument
source ./same_process1.sh "$number"
Note: Follow the same procedures above to create and save a new script namely same_process1.sh

Script (same_process1.sh) >

#!/bin/bash

# Prompt the user to enter another number
read -p "Please enter another number: " number2

# let's perform some operations with the numbers
result=$((number * number2))

# Display the result
echo "The multiplication of the numbers is: $result"
EXPLANATION

In this demonstration, same_process.sh prompts the user for a number, reads it into the number variable, and uses the source command to execute same_process1.sh with number as an argument. The same_process1.sh accesses the number, prompts for another number and performs multiplication using both numbers and displays the result. The source command enables passing variables and maintaining the state between the scripts.

❹ Use the following two commands to make both file executable:

chmod u+x same_process.sh
chmod u+x same_process1.sh
EXPLANATION
  • chmod: Changes permissions.
  • u+x: Giving the owner executing permission.
  • same_process.sh: Name of the script.
  • same_process1.sh: Name of the script.
Changing Permission❺ Run the same_process.sh script by the following command:
./same_process.sh

Calling another script in same processHere, you can see the user inputs 10 when the same_process.sh  prompts the user to input a number. After that, the script moves to another script namely same_process1.sh. This script prompts the user to input another number and the user again inputs 10. Then the program performs a multiplication operation and shows the result 100. The important thing to note here, using the source command you can access the variable and functions of the first script.

Note: Instead of the source command, you can use the dot(.) operator. The dot operator is equivalent to the source command. Moreover, you can provide the absolute path of the second script after the source command as well as a path relative to the current directory.

Method 2: Call and Run Another Script in a Different Process

You can call another Bash script in a completely different process than the running script. This way you can not access the variables and functions of the calling script in the main script.

Case 1: Use of “bash” Command

For this purpose, you can use the bash command or add the path of the calling script in the PATH variable. I am going to show you both processes.

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Script (diff_process1.sh) >

#!/bin/bash
# Call and run another script in a different process
echo "This program will give the multiplication of two numbers."
bash ./diff_process2.sh

Script (diff_process2.sh) >

#!/bin/bash

read -p "Please enter a number: " number1
read -p "Please enter another number: " number2
result=$((number1 * number2))
echo "The multiplication of the numbers is: $result"
EXPLANATION

The code includes two scripts diff_process1.sh and diff_process2.sh. diff_process1.sh initiates the execution of diff_process2.sh in a separate process using the bash command. Before executing diff_process2.sh, it displays a message informing the user that the program will calculate the product of two numbers.

In diff_process2.sh, the user is prompted to input two numbers, which are stored in the variables number1 and number2. The script then performs the multiplication and displays the calculated multiplication result to the user.

Run the main script by the following command:

./diff_process1.sh

Calling another script in different processHere, the main script displays a message informing you that the program will calculate the product of two numbers. After that, it starts to execute the diff_process2.sh file and prompts the user to input numbers. The user first inputs 10 and then 5. Then the program displays the multiplication result 50 as you can see in the image.

Note: Make sure both of the scripts are executable.

Case 2: Use of “export” Command to Call Another Script in Bash

Other way of calling another script in a different process is setting the path of the calling script in the PATH variable using the export command.

You can follow the Steps of Method 1 to learn about creating and saving shell scripts.

Script (path1.sh) >

#!/bin/bash

echo "This program will give the multiplication of two numbers."

# Add the path of path2.sh to $PATH
export PATH="$PATH:/home/laku"
# Call path2.sh as a normal command
path2.sh

Script (path2.sh) >

#!/bin/bash

read -p "Please enter a number: " number1
read -p "Please enter another number: " number2
result=$((number1 * number2))
echo "The multiplication of the numbers is: $result"
EXPLANATION

The code includes two scripts path1.sh and path2.sh. path1.sh initiates the execution of path2.sh in a separate process using the export command. It sets the value of the PATH variable to /home/laku which contains the path2.sh script.

Before executing path2.sh, it displays a message informing the user that the program will calculate the product of two numbers using the echo command.

In path2.sh, the user is prompted to input two numbers, which are stored in the variables number1 and number2. The script then performs the multiplication and displays the calculated result to the user.

Run the main script by the following command:

./path1.sh

export command for calling another script in BashThis time the main script displays the message that the program will calculate the product of two numbers. Then it moves to the script path2.sh. Later it seeks numbers from the user to input. The user first inputs 4 and then 5. Then the program displays the multiplication result 20 as you can see in the image.

Note: Make sure both of the scripts are executable.

Comparative Analysis of Methods

Here is a comparative analysis between the above-discussed methods. Take a look at the analysis to determine which method suits your needs best.

Method Advantage Disadvantage
Running in the same process
  • Variables and functions of the calling script are accessible.
  • Save the time and resources needed for process creation and termination.
  • Error in one script affects the other.
Running in a different process
  • Error in one script doesn’t crash the execution of the other.
  • Variables and functions of the calling script are not accessible.

The choice between the two methods depends on the specific situation and it effects the script execution. I prefer running in a separate process because it is safer. However, if there is a need to access variables and functions from the calling script, running in the same process becomes necessary. The decision should be made based on the specific requirements and objectives.

Why Calling Another Script in Bash Not Working?

Calling another script in the running script may end up with an error due to insufficient knowledge of how a process runs. For example, look at the script below.

Script (issue1.sh) >

#!/bin/bash

echo "This program will give the multiplication of two numbers."
# Prompt the user to enter a number
read -p "Please enter a number: " number1
# Execute another script and pass the number>
export PATH="$PATH:/home/laku"
issue2.sh

Script (issue2.sh) >

#!/bin/bash

# Prompt the user to enter another number
read -p "Please enter another number: " number2

# let's perform some operations with the numbers
result=$((number1 * number2))

# Display the result
echo "The multiplication of the numbers is: $result"
EXPLANATION

The code includes two scripts issue1.sh and pissue2.sh. issue1.sh initiates the execution of issue2.sh in a separate process using the export command. It sets the value of the PATH variable to /home/laku which contains the issue2.sh script.

Before executing issue2.sh, it displays a message informing the user that the program will calculate the product of two numbers using the echo command and seeks the first number.

In issue2.sh, the user is prompted to input another number. Due to running in a different process, the program will not perform the calculation as it can’t access the number1 variable of the first script.

Run the main script by the following command:

./issue1.sh

Error in calling another script in BashThe expected result of the multiplication is 25. Though the issue1.sh calls the issue2.sh script properly but the program can’t perform the calculation. Because once the issue2.sh starts running it immediately terminates the process of issue1.sh script. Hence the number1 variable is not accessible from issue2.sh script.

Conclusion

In summary, calling another script in Bash is essential in many cases. I believe now you have a clear idea of how to call another script in a different process as well as with the running process of a script. I appreciate your valuable suggestions regarding this article. Thanks for reading.

People Also Ask

How to call another script with parameters?
To call another script with parameters add the parameters after the path of the script.
Can I call another script if the calling script is not relative to the current directory?
Yes you can. In that case, give the absolute path of the calling script.
Can I use exec to run a sub-script in Bash?
Yes. However, you must have a clear understanding of what you want to achieve using the exec command.

Related Articles


<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

5/5 - (1 vote)
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment