Skip to content
Latchkey

envsubst Command Reference for CI Scripts

envsubst replaces ${VAR} references in text with values from the environment.

envsubst renders config templates at deploy time, filling in environment-specific values. The danger is that by default it substitutes every variable it recognizes, including ones you meant to leave literal.

Common flags/usage

  • envsubst < template > output: substitute all environment variables
  • envsubst '$VAR1 $VAR2': restrict to a named list of variables
  • reads stdin, writes stdout
  • only ${VAR} and $VAR forms are replaced
  • ships with gettext, sometimes a separate package

Example

shell
export APP_PORT=8080 APP_ENV=production
envsubst '$APP_PORT $APP_ENV' < app.conf.tmpl > app.conf

In CI

Without the quoted variable-list argument, envsubst replaces every $-reference, so a literal $ in an nginx config or a shell snippet inside the template gets blanked out; always pass an explicit list like '$APP_PORT $APP_ENV'. envsubst comes from gettext and may need installing (apt-get install gettext-base) on slim images.

Key takeaways

  • Pass an explicit "$VAR" list so envsubst does not blank out unrelated $ references.
  • envsubst reads stdin and writes stdout; redirect a template through it.
  • It ships with gettext and may need installing on minimal images.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →