Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

envsubst

Fill $VARIABLES in a template file with the values of environment variables — a tiny template engine for CI and containers.

Level
DevOps tool
Group
Text and search
Needs root
No
Package
gettext

What it does

envsubst reads text, replaces $NAME and ${NAME} with the value of the environment variable NAME and prints the result. It is used to make config files from templates: a Kubernetes manifest with the image tag, an nginx config with the server name, a .env file per stage.

Only exported variables are seen. Give a list of variables (the SHELL-FORMAT) to replace only those and leave others (like $host in nginx) alone.

How to write it

envsubst ['$VAR1 $VAR2'] < TEMPLATE > OUTPUT

Options 3

Most used

Options

'SHELL-FORMAT'
Replace only the variables named in this string, for example '$TAG $ENV'.
-v, --variables
Print the names of the variables used in SHELL-FORMAT.
--help / --version
Help / version.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Fill a Kubernetes manifest in a pipeline

    cat deploy.tpl.yaml
    image: ghcr.io/acme/shop:${TAG}
    replicas: ${REPLICAS}
    export TAG=v1.4.0 REPLICAS=3
    envsubst < deploy.tpl.yaml | kubectl apply -f -
  2. 2Replace only some variables (keep nginx's own $host)

    export SERVER_NAME=shop.example.com
    envsubst '$SERVER_NAME' < nginx.conf.tpl > /etc/nginx/conf.d/shop.conf

    Without the list, $host and $remote_addr would become empty.

  3. 3Container entrypoint that writes the config at start

    cat entrypoint.sh
    #!/bin/sh
    envsubst '$API_URL' < /etc/app/config.tpl.json > /etc/app/config.json
    exec "$@"
  4. 4The official nginx image does this for you

    docker run -e SERVER_NAME=shop.example.com -v ./templates:/etc/nginx/templates nginx:1.27

    Files in /etc/nginx/templates/*.template are filled into /etc/nginx/conf.d/ at start.

  5. 5A .env file per stage

    export DB_HOST=db.prod.internal
    envsubst < .env.tpl > .env.prod
  6. 6See which variables a template uses

    envsubst -v "$(cat deploy.tpl.yaml)"
    TAG
    REPLICAS
  7. 7One line

    NAME=world envsubst <<< 'Hello $NAME'
    Hello world
  8. 8Fail if a variable is missing (check first)

    for v in TAG REPLICAS; do [ -n "${!v}" ] || { echo "missing $v"; exit 1; }; done
  9. 9Use with heredoc

    export PORT=8080
    envsubst <<'EOF2' > app.conf
    listen = 0.0.0.0:${PORT}
    EOF2
  10. 10Fill all templates of a folder

    for f in k8s/*.tpl.yaml; do envsubst < "$f" > "${f%.tpl.yaml}.yaml"; done

Watch out

  • A variable that is not set becomes an empty string, without an error.
  • Variables must be exported (export VAR=…), plain shell variables are not seen.
  • envsubst knows only $VAR and ${VAR} — no defaults like ${VAR:-x}.

Tips to remember

  • For anything bigger (loops, conditions) use Helm, Kustomize or a real template tool.

Exit codes

0OK.
1Error.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man envsubst