While I had no particular reason why to code it in Perl or Python, I ended writing my scripts in Bash on this Sunday afternoon. No huge tasks were meant to be executed, just some database reading. However, all of the short scripts would be using common variables, namely regarding the DB credentials. Sourcing variables from an external file could be achieved using . filename or source filename.

A little heck comes when passwords containing special characters such as $ < > [] {} ` ' " \ | & ; * ? are used. In your source file you need to escape each of those characters using a backslash \ symbol.

For example we could have a configuration file db.conf as follows:

HOST=localhost
DBNAME=testdb
DBUSER=user001
DBPASS=\[email protected]\$

Now, let’s get the variables as follows:

#!/bin/bash
. db.conf
echo $DBPASS

Copy the above in a file named getSQLPass.sh and we run it.

bash-source-file

The backslash symbols do not get printed.