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:


DB_HOST=127.0.0.1

DB_PORT=3306

DB_USER=testuser

DB_PASS=test123

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


#!/bin/bash

CONF="config.conf"

. $CONF

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


#!/bin/bash

CONF="config.conf"

source $CONF

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

Leave a Comment

Back to top