Bash Call Another Script

5 min read Oct 10, 2024
Bash Call Another Script

How to Execute Another Script from a Bash Script

In the realm of scripting, the ability to call another script from within a current script is a fundamental yet powerful technique. Bash, the default Unix shell, offers a straightforward way to achieve this. Let's explore how to effectively use this functionality.

Understanding Script Execution

Before delving into the intricacies of calling another script, let's grasp the basic concepts:

  • Bash Script: A Bash script is a text file containing a series of commands that are executed sequentially by the Bash shell.
  • Script Execution: Executing a script entails running these commands in a specific order.
  • Calling Another Script: This refers to invoking the execution of a different script file from within the currently running script.

The source Command

One commonly employed method for calling another script is the source command. It executes the specified script in the current shell environment.

Syntax:

source 

Example:

Let's say you have two scripts:

  • script1.sh:
#!/bin/bash

echo "This is script1"
  • script2.sh:
#!/bin/bash

echo "This is script2"

source ./script1.sh

When you run script2.sh, it will print:

This is script2
This is script1

The source command in script2.sh executes script1.sh within the same shell environment, resulting in the output of both scripts.

The . Command

Another approach, often used interchangeably with source, is the . command. Its syntax is similar:

Syntax:

. 

Example:

#!/bin/bash

echo "This is script3"

. ./script1.sh 

Running script3.sh produces the same output as the previous example.

Passing Arguments

You can pass arguments to the called script just like you would with any command.

Example:

#!/bin/bash

echo "This is script4"

./script1.sh "Hello from script4"

In this case, script1.sh will receive the argument "Hello from script4".

The bash Command

For scenarios where you need a separate shell environment, you can use the bash command followed by the script path. This creates a new shell process to execute the script.

Syntax:

bash 

Example:

#!/bin/bash

echo "This is script5"

bash ./script1.sh

This approach will execute script1.sh in a separate shell, keeping the environment of script5.sh unaffected.

Choosing the Right Approach

When deciding which method to use, consider the following:

  • source or .: Choose these when you want to modify the current shell environment and access variables defined within the called script.
  • bash: Opt for this when you need a separate shell environment, preventing any potential conflicts or side effects on the current shell.

Considerations

  • Path: Ensure that the script you are calling is located in a directory accessible from your current script. If not, provide the complete path.
  • Permissions: The script you are calling must have execute permissions.

Conclusion

Understanding how to call another script from a Bash script is crucial for building complex and modular scripting solutions. The source and . commands allow you to modify the current shell environment, while the bash command creates a separate shell for script execution. By applying these techniques effectively, you can streamline your scripting workflow and create powerful automation solutions.