Fork me on GitHub

Project Notes


substitute shell variables in text


envsubst is is available on most *nix systems and can be installed on MacOS as part of the BSD gettext library.

It’s a useful tool for either completely or selectively substituting environment variables in text that has shell format strings.

Example uses include:

  • generating config files from templates with variable substitution

Quick Example

envsubst will attempt to substitute all variable by default:

$ echo 'substituting ${TEST} ${ALT} ${NADA}' | TEST=abc ALT=alt envsubst
substituting abc alt

Or you can give it a constrained list of variables to interpolate:

$ echo 'substituting ${TEST} ${ALT} ${NADA}' | TEST=abc ALT=alt envsubst '$TEST:$ALT'
substituting abc alt ${NADA}

MacOS Installation

Easy to install with homebrew:

$ brew install gettext

But note the formula is keg-only, which means it was not symlinked into /usr/local. So while you could directly reference /usr/local/opt/gettext/bin/envsubst or add that to the path, brew link can be used to add the symlinks:

$ brew link --force gettext
$ which envsubst

When done, you can unlink if desired:

$ brew unlink gettext

Credits and References

About LCK#288 ToolsBash
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.