Heredoc Templating (Generating a Config File from Variables)
In this page:
Generating a Config File from Variables
Because the heredoc delimiter here is unquoted, every $variable inside the block is expanded to its current value before being written out, effectively turning the heredoc into a template.
Example: Generating a Config File from Variables
#!/bin/bash
host="db.example.com"
port=5432
user="app_user"
cat > db_config.ini <<EOF
[database]
host=$host
port=$port
user=$user
EOF
cat db_config.ini
rm -f db_config.ini
Login to try C/C++/Java/PHP code in the editor
Including Command Substitution in a Template
Because expansion happens the same way it does in double-quoted strings, $(command) inside an unquoted heredoc runs the command and inserts its output, useful for stamping a generated file with a timestamp or hostname.
Example: Including Command Substitution in a Template
#!/bin/bash
version="1.2.0"
cat > release_notes.txt <<EOF
Release: $version
Generated year: $(date +%Y)
EOF
cat release_notes.txt
rm -f release_notes.txt
Login to try C/C++/Java/PHP code in the editor
Validating Required Variables Before Templating
Checking that every variable the template needs is actually set beforehand avoids silently generating a config file with blank or missing values, which can be a hard-to-diagnose bug later.
Example: Validating Required Variables Before Templating
#!/bin/bash
host="localhost"
port=""
if [[ -z $host || -z $port ]]; then
echo "missing required template variable(s), skipping generation" >&2
else
cat > out.ini <<EOF
host=$host
port=$port
EOF
fi
echo "validation check complete"
Login to try C/C++/Java/PHP code in the editor
- Quoting the heredoc delimiter (
<<EOF) when you actually wanted variable substitution, which produces literal$vartext instead of the expanded value. - Forgetting that command substitution and arithmetic expansion also happen inside an unquoted heredoc, not just simple variable expansion.
- Not validating that all required variables are set before generating a config file, which can silently produce a broken file with empty values.
- An unquoted heredoc delimiter allows variables, command substitution, and arithmetic expansion inside the block.
- Redirecting a heredoc into a file with
> filenameis a simple, readable way to generate config files from a template. - Combine heredoc templating with
set -u(or explicit checks) so a missing variable causes a clear error rather than an empty gap in the generated file. - Nested double quotes inside an unquoted heredoc don't need escaping the way they would inside a quoted string.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- getopts for Parsing Command-Line Flags
- Positional Parameters and shift
- Process Substitution in Practice (diff <(...) <(...))
- Arrays of Arguments ("$@" vs "$*" Quoting Difference)
- Nameref Variables (declare -n) for Passing Arrays by Reference
- Heredoc Templating (Generating a Config File from Variables)
- Default/Fallback Argument Handling