Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

envsubst

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

Niveau
DevOps-tool
Groep
Tekst en zoeken
Root nodig
Nee
Pakket
gettext

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

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.

Zo schrijf je het

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

Opties 3

Meest gebruikt

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.

Voorbeelden 10

$ als gewone gebruiker# als root (of met 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

Let op

  • 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 om te onthouden

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

Exitcodes

0OK.
1Error.

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man envsubst