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.
Make sure that script2.sh is executable. You can set the executable permission using the following command:
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, .).
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:
And script2.sh can access these arguments as $1, $2, etc.
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.
To handle return values (exit status), use the $? variable, which stores the exit status of the last command executed.
Key Points Summary
| Method | Usage | Scenario |
| Direct Call | ./script2.sh | When scripts run independently. |
| Sourcing Scripts | source ./script2.sh or . ./script2.sh | When sharing variables, functions between scripts. |
| Calling with Arguments | ./script2.sh "arg1" "arg2" | Passing variables or runtime parameters to scripts. |
| Capturing Output | output=$(./script2.sh) | Needed when script output must be processed. |
| Handling Return Values | if [[ $? -eq 0 ]]; then | Check 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.

