[Bash] Writing configuration files and consuming them
by Riley MacDonald, January 10, 2018

I wanted to access key/value pairs from a configuration file for a bash script. I accomplished this using the following technique:

# Create the configuration file
 
$ touch myconfig.file

Using your preferred text editor, add the desired configurations. For example:

1
2
3
4
# vim myconfig.file
 
sampleKey=sampleValue
anotherVar=anotherVal

Now source the file within your script and consume the data:

1
2
3
4
5
6
7
# sampleScript.sh
 
# source the configuration file created above
source /path/to/myconfig.file
 
# access the data
echo $sampleKey

Happy scripting and shelling.

Open the comment form

Leave a comment:

Comments will be reviewed before they are posted.

User Comments:

Be the first to leave a comment on this post!