FUNDAMENTALS A Complete Guide for Beginners
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.
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.
❶ 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
- nano: Opens a file in the Nano text editor.
- same_process.sh: Name of the 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"
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"
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
- chmod: Changes permissions.
- u+x: Giving the owner executing permission.
- same_process.sh: Name of the script.
- same_process1.sh: Name of the script.
./same_process.sh
Here, 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.
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.
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"
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
Here, 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.
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.
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"
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
This 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.
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 |
|
|
Running in a different process |
|
|
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"
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
The 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
Related Articles
- How to Get Date in Bash [2 Methods with Examples]
- How to Print Time in Bash [2 Quick Methods]
- How to List Users in Bash [2 Easy Ways]
- How to Get Current Time in Bash [4 Practical Cases]
- How to Use Date Format in Bash [5 Examples]
- How to Get Timestamp in Bash [2 Practical Cases]
- How to Copy and Paste in Bash [2 Methods & Cases]
- How to Read Password in Bash [3 Practical Cases]
- How to Send Email in Bash [2 Easy Methods]
- Bash Script to Send Email with Attachment [Step-by-Step Guide]
- How to Get IP Address in Bash [3 Methods]
- How to Find and Replace String in Bash [5 Methods]
- How to Get Script Name Using Bash Script? [3 Easy Ways]
- How to Generate UUID in Bash [3 Simple Methods]
- 3 Easy Ways to Write to a File in Bash Script
- How to Write the Output to a File in Bash Script [5 Practical Cases]
- How to Create a List in Bash Scripts? [2 Easy Methods]
- How to Clear History in Bash [2 Practical Cases]
- How to Clear Screen Using Bash Script? [2 Effective Methods]
- How to Check Ubuntu Version Using Bash Scripts? [2 Methods]
<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial