How to Include Script File in Another Bash Script

Faaiq Bilal Mar 14, 2025 Bash Bash File
  1. Using the source Command
  2. Using the . Command
  3. Using Command Substitution
  4. Using Here Documents
  5. Conclusion
  6. FAQ
How to Include Script File in Another Bash Script

Including one Bash script in another can streamline your coding process, making your scripts cleaner and more manageable. Whether you’re working on a complex project or simply want to reuse code, understanding how to include scripts is essential.

This article explores various methods to include a Bash script file into another script file, focusing on practical examples and explanations. By the end, you’ll have a solid grasp of how to effectively integrate scripts, enhancing your coding efficiency and organization. So, let’s dive into the different methods you can use to include a Bash script in another script.

Using the source Command

One of the most straightforward methods to include a Bash script in another is by using the source command. This command executes the contents of a script in the current shell environment, allowing you to access variables and functions defined in the included script.

Here’s a simple example to illustrate how the source command works:

# main_script.sh
#!/bin/bash
source ./included_script.sh
echo "The value of MY_VAR is: $MY_VAR"
# included_script.sh
#!/bin/bash
MY_VAR="Hello, World!"

When you run main_script.sh, it will include included_script.sh, which defines the variable MY_VAR. The output will be:

Output:

The value of MY_VAR is: Hello, World!

In this example, source loads included_script.sh into the current shell. This means that any variables or functions declared in included_script.sh become available in main_script.sh. This method is particularly useful for setting up configuration files or utility functions that you want to reuse across multiple scripts.

Using the . Command

The dot command (.) is another way to include a script file in Bash. It functions similarly to the source command, executing the contents of the script in the current shell. The syntax is slightly different, but the result is the same.

Here’s how you can use the dot command:

# main_script.sh
#!/bin/bash
. ./included_script.sh
echo "The value of MY_VAR is: $MY_VAR"
# included_script.sh
#!/bin/bash
MY_VAR="Hello, World!"

When you execute main_script.sh, the output will be identical to the previous example:

Output:

The value of MY_VAR is: Hello, World!

Using the dot command is a matter of preference, as it performs the same function as the source command. It’s worth noting that many Bash users prefer the source command for readability, while others may opt for the dot command for brevity. Regardless of your choice, both methods are effective for including scripts.

Using Command Substitution

Another approach to include a Bash script in another is through command substitution. This method allows you to run a script and capture its output within another script. While this doesn’t directly include variables or functions, it can still be useful for processing data or executing commands.

Here’s an example:

# main_script.sh
#!/bin/bash
RESULT=$(./included_script.sh)
echo "The output from included_script.sh is: $RESULT"
# included_script.sh
#!/bin/bash
echo "Hello, World!"

When you run main_script.sh, the output will be:

Output:

The output from included_script.sh is: Hello, World!

In this case, main_script.sh executes included_script.sh and captures its output into the variable RESULT. This method is particularly useful when you want to process or manipulate the output of another script without directly including its contents. However, keep in mind that variables and functions defined within included_script.sh will not be accessible in main_script.sh using this method.

Using Here Documents

Here documents are a powerful feature in Bash that allows you to include multi-line strings directly in your script. While not a direct inclusion of another script, it can be used to embed script content within your main script, making it easier to manage.

Here’s an example of using here documents:

# main_script.sh
#!/bin/bash
cat << 'EOF' > included_script.sh
#!/bin/bash
echo "Hello from included_script.sh!"
EOF

chmod +x included_script.sh
./included_script.sh

When you run main_script.sh, the output will be:

Output:

Hello from included_script.sh!

In this example, main_script.sh creates included_script.sh using a here document and then executes it. This method is beneficial when you want to generate scripts dynamically or include small snippets of code without creating separate files. However, for larger or reusable scripts, it’s generally better to keep them in separate files.

Conclusion

Incorporating one Bash script into another can significantly enhance your coding efficiency and organization. Whether you choose to use the source command, the dot command, command substitution, or here documents, each method has its unique advantages. By mastering these techniques, you can streamline your workflow and make your scripts more modular and maintainable. Remember, the method you choose will depend on your specific needs and coding style. Happy scripting!

FAQ

  1. What is the difference between the source command and the dot command?
    The source command and the dot command perform the same function of including a script in the current shell. The main difference is their syntax; source is more readable, while . is more concise.

  2. Can I include scripts from different directories?
    Yes, you can include scripts from different directories by providing the relative or absolute path to the script file.

  3. What happens to variables defined in the included script?
    Variables defined in the included script are available in the calling script when using the source or dot command. However, they won’t be accessible if you use command substitution.

  4. Is it a good practice to include scripts in Bash?
    Yes, including scripts can help you avoid code duplication, enhance readability, and make your scripts easier to manage.

  5. How do I make an included script executable?
    You can make a script executable by using the command chmod +x script_name.sh.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe

Related Article - Bash File