techbits.io

The simple way to add a Config File to your Bash Scripts

Source lets you add external files/code into your scripts, but is also a convenient way of adding config files, providing you are aware of the risks.

The simple way to add a Config File to your Bash Scripts

The benefits are that source is extremely quick to run, with memorable syntax.

The risks are that all code in the file(s) you source will be executed, even if you only anticipated the file holding variables.

In short, if there’s any chance of anyone else adding/editing the config files (a curious user, perhaps), then source isn’t the right option.

Let’s say you have a config file (config.ini) that contains your_var=your_val.

In your script, you can use source or the shorthand . syntax

. config.ini
# or: source config.ini

echo $your_var 

>>> your_val

But if anyone can edit config.ini, and they add executable commands… Well just hope they are messing with you and add a relatively painless reboot, rather than rm -rf /. You’ve been warned!

Share this article:

Comments

Related posts

Autologin and continue running a script in Linux
#linux #scripting

Autologin and continue running a script in Linux

I'm going to prefix this whole post with: Computers/servers automatically logging in is bad security practice. Certain situations may require this…

Create macOS Terminal aliases
#macos #scripting

Create macOS Terminal aliases

Aliases in the Terminal are shortcuts to run commands which you might find yourself using a lot, or they could be longer commands that you use infrequently but are more likely to get wrong. Aliases can even be used to override default commands!