Envsubst

Envsubst

Big templating engines like Jinja2 for Python are really useful tools. But sometimes, you just need to do a little templating in a shell script, to compose a config file, for example.

Envsubst is a simple templating machine for the shell. It replaces environment variables in files. A simple example says more than a thousand words:

$ cat .gitconfig-demo
[user]
  email = $GIT_EMAIL
  name = $GIT_USER

$ export GIT_EMAIL=nik@drnik.org
$ export GIT_USER=Nik

$ envsubst < .gitconfig-demo
[user]
  email = nik@drnik.org
  name = Nik

Of course, to use it, you have to fill your shell environment with variables (you may even say, you have to pollute it). On the other hand, you should prefix your environment variables (or make them unique in some other way), so not much harm done. On the plus side, the thing is dead simple and may be all you need in many situations.

It seems to come with a standard Linux (or even Unix) setup, so no extra installation needed.

links

social