Introduction
Reading configuration files lets your script use settings without changing the script itself. It helps keep things organized and easy to update.
source /path/to/config_file
# or
. /path/to/config_filesource ./myconfig.cfg
. /etc/myapp/config.cfg
# Inside config file:
USERNAME=admin
PASSWORD=secret#!/bin/bash # Load config file source ./app.config # Use variables from config echo "Connecting to database at $DB_HOST with user $DB_USER"
settings.conf in a bash script?source or . is used to read and execute commands from a file in the current shell.read reads input, import is not a bash command, and execute is invalid.app.conf with content:USERNAME=guest PORT=8080
source app.conf echo "User: $USERNAME, Port: $PORT"
source app.conf command loads variables USERNAME and PORT into the script.config.cfg and print a variable?source config.cfg echo "Value: $VALUE"
config.cfg contains VALUE=42, but the output is empty.VALUE = 42, bash treats it as a command, not assignment.settings.conf that might contain unsafe commands. Which approach is best to avoid running unwanted code while still getting variable values?source runs all commands in the file, which can be unsafe if file is untrusted.Pass the test and this pattern moves unknown