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.confIn 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
sed Command Reference for CI Scriptssed is a stream editor for find-and-replace in CI builds. Reference for -i, -e, -E, and s///, plus the GNU-vs…
printf Command Reference for CI Scriptsprintf formats output predictably in CI, unlike echo. Reference for format specifiers, %s, %b, and escapes, p…
jq Command Reference for CI Scriptsjq is the command-line JSON processor for CI. Reference for -r raw output, -e exit status, --arg, and filters…
base64 Command Reference for CI Scriptsbase64 encodes and decodes data for transporting secrets in CI. Reference for -d, -w0, and stdin usage, plus…