How can I externalize the template part of a "heredoc"?
I have a bash script that uses a "heredoc" to write data to a file:
foo=bar
data=$(cat <<EOF
foo: $foo
EOF
)
echo $data
When executing this script, I see the expected foo: bar
.
Now, I would like to pull out the template part from the script to support multiple output formats using different template files. For example, a template.properties
file containing just:
foo: $foo
For this reason I modified the script:
foo=bar
template=$(cat template.properties)
data=$(cat <<EOF
$template
EOF
)
echo $data
The problem I am having is that the $foo
variable never gets evaluated, i.e. the above code prints out foo: $foo
instead of the desired foo: bar
.