How to Call Another Script From Current Script in Bash

  1. Using the source Command
  2. Using the . Command
  3. Using the Execute Permission
  4. Using Command Substitution
  5. Conclusion
  6. FAQ
How to Call Another Script From Current Script in Bash

Bash scripting is a powerful way to automate tasks and streamline workflows on Unix-like operating systems. One common requirement in scripting is the need to call another script from your current script. This can enhance modularity and reusability, allowing you to break down complex tasks into simpler, manageable scripts.

In this tutorial, we will explore various methods to run another script from your current Bash script. By the end, you’ll have a solid understanding of how to effectively manage script execution in Bash, making your scripting endeavors more efficient and organized.

Using the source Command

One of the simplest ways to call another script from your current script is by using the source command. This command executes the contents of the specified script in the current shell environment. It allows you to access variables and functions defined in the called script without creating a new subshell.

Here’s how to use the source command:

#!/bin/bash

echo "This is the main script."

source ./another_script.sh

echo "Back to the main script."

Output:

This is the main script.
Hello from another script!
Back to the main script.

In this example, the source command executes another_script.sh within the context of the main script. Any variables or functions defined in another_script.sh remain accessible after it runs. This is particularly useful when you want to share variables among scripts or define utility functions in a separate file that can be reused across multiple scripts.

Using the . Command

The dot command, which is simply a shorthand for the source command, serves the same purpose in Bash. It allows you to execute another script in the current shell environment. The syntax is very similar, making it an easy alternative.

Here’s an example of using the dot command:

#!/bin/bash

echo "Starting the main script."

. ./another_script.sh

echo "Returning to the main script."

Output:

Starting the main script.
Hello from another script!
Returning to the main script.

In this case, the dot (.) before the script path tells Bash to execute another_script.sh in the current shell. The output will be the same as when using the source command. This method is often preferred for its brevity, especially in scripts where space is a consideration. Just like with the source command, any variables or functions defined in another_script.sh will be available for use after it is called.

Using the Execute Permission

Another method to call another script from your current script is by executing it directly, provided it has the appropriate execute permissions. This method runs the script in a new subshell, meaning any variables defined in the called script will not affect the parent script.

Here’s how to do it:

#!/bin/bash

echo "Executing the main script."

./another_script.sh

echo "Back to the main script."

Output:

Executing the main script.
Hello from another script!
Back to the main script.

In this example, another_script.sh is executed as a separate process. This means that while you can run the script and see its output, any variables or functions defined in another_script.sh will not be available in the main script after it runs. This method is useful when you want to ensure that the called script runs independently without affecting the current script’s environment.

Using Command Substitution

Command substitution allows you to capture the output of another script and use it in your current script. This can be particularly useful for scripts that perform calculations or return specific values.

Here’s an example:

#!/bin/bash

echo "Starting the main script."

result=$(./another_script.sh)

echo "The result from another script is: $result"

Output:

Starting the main script.
Hello from another script!
The result from another script is: 42

In this example, the output of another_script.sh is captured in the result variable. This method is especially handy when you want to use the output of the called script later in your main script. The command substitution syntax $(...) is a powerful feature of Bash that can help you create dynamic and responsive scripts.

Conclusion

In this tutorial, we explored several methods to call another script from your current script in Bash. Whether you choose to use the source command, the dot command, execute permissions, or command substitution, each method offers unique advantages depending on your scripting needs. By understanding these techniques, you can enhance the modularity and efficiency of your Bash scripts, making them easier to maintain and expand. As you continue to work with Bash, remember that mastering script interactions is key to becoming a proficient shell programmer.

FAQ

  1. What is the difference between using source and executing a script?
    Using source runs the script in the current shell, allowing access to its variables and functions, while executing a script runs it in a new subshell, isolating its environment.

  2. Can I pass arguments to the script I am calling?
    Yes, you can pass arguments to the script you are calling, just like you would with any command in Bash.

  3. What happens if the called script fails?
    If the called script fails, it will typically stop the execution of the main script unless you handle the error using conditional statements.

  4. Is it necessary to give execute permissions to a script I want to run?
    Yes, if you are executing a script directly (not using source or dot), it must have execute permissions.

  1. Can I call multiple scripts from one script?
    Absolutely! You can call multiple scripts from a single script using any of the methods discussed.
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Bash Script