Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Shell trick: use template file

If you want to use a POSIX shell for building files from templates, you can use a nifty trick shared on StackOverflow.

$ eval "cat <<EOF >outputfile
$( cat template.in )
EOF
" 2>/dev/null

And template.in can contain shell variables or even command substitution!

$ cat template.in
[domain/$( hostname --domain )]

debug_level = 1
id_provider = ipa
ipa_server = _srv_, $( hostname --domain )
ipa_domain = $( hostname --domain )
ipa_hostname = $( hostname --fqdn )

This is a neat little trick, but should not be a regular substitute for better templating mechanisms like the jinja2 in ansible.

Comments