Shell Scripting
Scripting Tutorial
Programming
Linux Commands
Bash Scripts

How to call one shell script from another shell script?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Calling one shell script from another is a common practice in Unix-like systems where shell scripting is heavily utilized for automating tasks. This is particularly useful in situations where a complex problem is decomposed into simpler, more manageable modules, or when several scripts need to share common code. Below, we explore different methods and best practices for accomplishing this.

Method 1: Direct Call

The simplest way to call one shell script from another is by directly invoking the child script from within the parent script. Assume script1.sh needs to call script2.sh.

bash
1#!/bin/bash
2# This is script1.sh
3
4echo "Executing script1.sh"
5
6# Direct call to another script
7./script2.sh
8
9echo "Back to script1.sh"

Make sure that script2.sh is executable. You can set the executable permission using the following command:

bash
chmod +x script2.sh

Method 2: Sourcing the Script

If the script to be called does not need to run as a separate process and you want to use variables or functions defined in the second script, you can source it using the source command (or its shorthand, .).

bash
1#!/bin/bash
2# This is script1.sh
3
4echo "Before sourcing script2.sh"
5
6# Source script2.sh
7source ./script2.sh
8
9# Or you can use ". ./script2.sh"
10
11echo "After sourcing script2.sh"

When you source script2.sh, all variables, functions, etc., defined in script2.sh will be available in script1.sh.

Method 3: Calling with Arguments

You might sometimes need to pass arguments from the parent script to the child script. Here is how you can do it:

bash
1#!/bin/bash
2# This is script1.sh
3
4# Calling script2.sh with arguments
5./script2.sh "Hello" "World"

And script2.sh can access these arguments as $1, $2, etc.

bash
1#!/bin/bash
2# This is script2.sh
3
4echo $1  # Prints Hello
5echo $2  # Prints World

Handling Output and Return Values

To handle the output from the child script, you can use command substitution or capture the output in a variable.

bash
1#!/bin/bash
2# This is script1.sh
3
4output=$(./script2.sh)
5echo "Output of script2.sh is: $output"

To handle return values (exit status), use the $? variable, which stores the exit status of the last command executed.

bash
1#!/bin/bash
2# Call script and then check if it succeeded
3./script2.sh
4if [[ $? -eq 0 ]]; then
5  echo "script2.sh executed successfully"
6else
7  echo "script2.sh failed"
8fi

Key Points Summary

MethodUsageScenario
Direct Call./script2.shWhen scripts run independently.
Sourcing Scriptssource ./script2.sh or . ./script2.shWhen sharing variables, functions between scripts.
Calling with Arguments./script2.sh "arg1" "arg2"Passing variables or runtime parameters to scripts.
Capturing Outputoutput=$(./script2.sh)Needed when script output must be processed.
Handling Return Valuesif [[ $? -eq 0 ]]; thenCheck success or failure of script executions.

Best Practices and Additional Considerations

  • Permissions: Ensure the called scripts have the necessary execute permissions.
  • Path Issues: Use absolute paths or handle the current working directory carefully in scripts.
  • Environment Variables: Be aware of how environment variables are handled in sourcing vs direct calling.
  • Error Handling: Implement robust error checking especially when dealing with the outputs and return statuses of child scripts.

Calling scripts from other scripts enables modular coding and reuse of code, handling complex scripting tasks more manageable and maintainable. Whether through direct calling, sourcing, or passing arguments, each method serves different needs and understanding these can help in building efficient scripting environments.


Course illustration
Course illustration

All Rights Reserved.