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 > OUTPUTOpties 3
Meest gebruikt
Options
Niets past bij dit filter.
Voorbeelden 10
$ als gewone gebruiker# als root (of met sudo)
1Fill a Kubernetes manifest in a pipeline
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.confWithout the list,
$hostand$remote_addrwould become empty.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 "$@"4The official nginx image does this for you
docker run -e SERVER_NAME=shop.example.com -v ./templates:/etc/nginx/templates nginx:1.27Files in /etc/nginx/templates/*.template are filled into /etc/nginx/conf.d/ at start.
5A .env file per stage
export DB_HOST=db.prod.internal envsubst < .env.tpl > .env.prod6See which variables a template uses
envsubst -v "$(cat deploy.tpl.yaml)" TAG REPLICAS7One line
NAME=world envsubst <<< 'Hello $NAME' Hello world8Fail if a variable is missing (check first)
9Use with heredoc
export PORT=8080 envsubst <<'EOF2' > app.conf listen = 0.0.0.0:${PORT} EOF210Fill all templates of a folder
for f in k8s/*.tpl.yaml; do envsubst < "$f" > "${f%.tpl.yaml}.yaml"; done
Niets past bij dit filter.
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
$VARand${VAR}— no defaults like${VAR:-x}.
Tips om te onthouden
- For anything bigger (loops, conditions) use Helm, Kustomize or a real template tool.
Exitcodes
0 | OK. |
1 | Error. |