How to Include Bash Script in other Bash Script

How to Include Bash Script in other Bash Script

Bash scripts are very useful for doing work easier. It also helps for task automation. This tutorial will help you to how to include bash script in other bash script.

Create Sample Scripts

For example, I am creating two scripts, first is config.sh which contains some variables. Second script is our main script main.sh, which includes first script and used variables defines there.

Step 1 – First Script (config.sh)

USERNAME="manju"
EMAIL="admin@linuxmasterswiki.com"

Main Script (main.sh)

#!/bin/bash

# Including config.sh, set filename with proper path.

source config.sh  

echo Welcome ${USERNAME}!
echo Your email is ${EMAIL}.

Step 2 – Execute Script

Let’s execute main.sh using terminal and watch the results.

[root@linuxmasterswiki ~]$ sh main.sh

Welcome manju!
Your email is admin@linuxmasterswiki.com.
 
Enjoyed this video?
How to Include Bash Script in other Bash Script
"No Thanks. Please Close This Box!"