`envsubst` is not POSIX, but my preferred tool for such things: https://linux.die.net/man/1/envsubst
it's part of `gettext`: https://www.gnu.org/software/gettext/manual/html_node/envsub...
There is likely already a simple templating program installed on your linux OS: envsubst. It is included as part of the gettext-base package on Ubuntu and installed by default I believe.
https://www.gnu.org/software/gettext/manual/html_node/envsub...
The templating language looks similar to capability to that provided by templ.
Would perl or ruby not suit your requirements?
Re:Notes on the M4 Macro Language (2008)
by luizfelberti on Friday April 03, 2020 @05:59PM
(#22774356)
Ruby most definitely wouldn't, it doesn't come pre-installed on almost any AMI/server/Docker Image/etc.
As for perl, it does come "with the system" more often than Ruby, but also not as much as bash.
Even if it did, I reckon bash is something that nowadays people are more accustomed to than perl, and would consider it more of a no brainer.
If you try using awk or sed, you will need to figure out a magical way to escape whatever arbitrary value you are trying to replace, so you don't break sed's regex or awk's syntax.
M4 was one of the last things I tried, and it was an utter disaster. If any of M4's magical characters collides with the syntax of the file you're trying to template: tough luck. And there is no way to escape your file for M4 either, it's syntax doesn't support that.
I eventually discovered that Bash 4.4+ has some built-in functions for "dumb" string substitution[1], which is exactly what's missing from the default POSIX tools.
Since then I wrote this little wrapper around that functionality, as a simple, single-file, dependency-free Bash script. If you find something like that useful, it's on the link below:
[0] https://github.com/luizberti/templ
[1] https://www.tldp.org/LDP/abs/html/string-manipulation.html