Bash Scripts - Load/Read/Include Settings from Another File

To include a script into another script, we use the source command. In it’s simplest form, the command is this:


Here’s an example. Our keys.cfg file has this:


While our has this:


#Directory the script is in (for later use)
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Load the backup settings
source "${SCRIPTDIR}"/keys.cfg

In our file we have sourced the file keys.cfg which contains configuration settings which we can now use.

If you attempt to execute that shell script from a location other than the one where your script is, it can’t find the include unless it’s in your path. As a workaround we have defined SCRIPTDIR and made the scripts relative to one another.

If the file you are including is in the same directory you can use dirname $0:

source $(dirname $0)/
echo "The main script"

An alternative to:

scriptPath=$(dirname $0)



.. the advantage being not having the dependence on dirname, which is not a built-in command (and not always available in emulators)