Variable substitution with envsubst

If you need to generate configuration files quickly based on templates, you can use envsubst to help you.

On Fedora, envsubst is part of the gettext package, which is installed by default.

Let’s imagine this template:

  - name: $name
    group: core
    url: "https://$name.wains.be/"
    interval: 5m
    conditions:
      - "[STATUS] == 200"
      - "[CERTIFICATE_EXPIRATION] > 48h"

Now export a variable:

export name=blog

Now run envsubst:

$ envsubst < config.yaml
  - name: blog
    group: core
    url: "https://blog.wains.be/"
    interval: 5m
    conditions:
      - "[STATUS] == 200"
      - "[CERTIFICATE_EXPIRATION] > 48h"

You can of course redirect the output to a file:

$ envsubst < config.yaml > blog.yaml
$ cat blog.yaml
  - name: blog
    group: core
    url: "https://blog.wains.be/"
    interval: 5m
    conditions:
      - "[STATUS] == 200"
      - "[CERTIFICATE_EXPIRATION] > 48h"



Thanks for reading this post!


Did you find an issue in this article?

- click on the following Github link
- log into Github with your account
- click on the line number containing the error
- click on the "..." button
- choose "Reference in new issue"
- add a title and your comment
- click "Submit new issue"

Your feedback is much appreciated! πŸ€œπŸΌπŸ€›πŸΌ

You can also drop me a line below!