Skip to content

How to Include Bash Script in other Bash Script

How to Include Bash Script in other Bash Script. Include content of one shell script to other shell script using source command.

On this page

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="rahul"
EMAIL="rahul@tecadmin.net"

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@tecadmin ~]$ sh main.sh

Welcome rahul!
Your email is rahul@tecadmin.net.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top