config file for your bash script

January 22nd, 2011 mysurface Posted in Bash, source | Hits: 115555 | 4 Comments »

Sometime you may wish to create a bash script to do something based on a config file, what actually was in your mind? How to implement the config file for your bash script?

First I think of using command ‘cut’, where I can create a list of tag=value, then uses ‘cut‘ to read out the tag and value. But that is so wrong, because I could just using command ‘source’ to do it instead.

Let say I have a config.ini


And I want my bash_script to read these variables and perform grep.

source config.ini


The keyword ‘source’ will import the variable from config.ini into my script, made them available for my use. The source line can be also replace it into this:

. config.ini

DOT(.) is equivalent to command ‘source’.

I can also source another bash script which contains if-else logics, the same way I source my config.ini.

But bare in mind, if your target source file contains ‘exit’, it will exit your parent script.

For example, I have a sourcing, where my contain exit.


echo ${HELLO}

When I try to execute the ./, my script immediately exit before it execute the ‘echo’.

I hope that this post will gives you an idea of using command ‘source’ to implement your config file for your bash script.

4 Responses to “config file for your bash script”

  1. It’s great tip! Can I translate it into spanish for my blog?

    Thank in advance.

  2. @nordri, no problem, just go ahead.

  3. I just was dealing with the same quesion of ‘sourcing’ the scripts for config.

    One thing to watch though is that your config will be treated as part of script, so your assignment

    will get evaluated for $hello variable (not defined), and PATTERN result in “*”.

    So in this case you may need to escape special chars:


  4. I just enjoy Linux although have no idea how it works……

Leave a Reply