Skip to main content

Configuration Files: Externalize Settings

Configuration files store settings. Read them. Use them.

Here's the thing: Config files separate settings from code. Use them.

Reading Config

# config.conf
HOST=example.com
PORT=8080

# script.sh
source config.conf
echo "$HOST"

My take: Source config files. Use variables.

.env Files

# .env
DB_HOST=localhost
DB_PORT=5432

# Load
set -a
source .env
set +a

My take: .env files are common. Use them.

Common Patterns

Config Function

load_config() {
[ -f "$1" ] && source "$1"
}

What's Next?

Now that you understand config files, let's talk about Reusable Modules.


Personal note: Config files seemed unnecessary at first. Then I used them. Now I use them always. They're useful. Use them.