Here's an alternative that I use, as it saves installing the entire gettext
package for just one program. I have this awk script, I call envtmpl
, it will swap any environment variable that looks like {{ENV-VAR}}
for the value of ENV-VAR
#! /usr/bin/awk -f
{ for (a in ENVIRON) gsub("{{" _ a _ "}}",ENVIRON[a]); print }
So
$ echo "My shell '{{SHELL}}' is cool" | envtmpl
My shell '/bin/bash' is cool
As you can see, if {{
& }}
aren't what you prefer, its really each to change and this script works fine with busybox
's awk
.
It's not going to be the world's fastest solution, but it's really easy to implement and I mostly run it to prepare config files, so speed is pretty irrelevant.
WARNING: The only major difference between this and envsubst
is that this will NOT alter variables where no value exists. That is {{HAS-NO-VALUE}}
will be left exactly as that, where as envsubst
will remove those (replace them with blank).
You can fix this by adding more code into the awk
, if you want.