How to include a file in a bash shell script

I had written few shell scripts for a project. All these scripts were using some common configuration parameters so I decided to put them in a common file and include it in the scripts file so that when I move the script files from the development to the production environment, I have to change the values in one file only.

My configuration file “config.conf” contained variables like:

1DB_HOST=127.0.0.1
2 
3DB_PORT=3306
4 
5DB_USER=testuser
6 
7DB_PASS=test123

I included the configuration file in my bash script in the following way:

1#!/bin/bash
2 
3CONF="config.conf"
4 
5. $CONF

You can also use the “source” command to include files as below:

1#!/bin/bash
2 
3CONF="config.conf"
4 
5source $CONF

The source command can be abbreviated as just a dot as done in the example before.

Leave a Comment

Back to top