envsubst
substitute environment variables in text
TLDR
Substitute environment variables in a file
SYNOPSIS
envsubst [-v] [-V] [--variables] [SHELL-FORMAT] [< input]
DESCRIPTION
envsubst substitutes environment variable references in shell format strings. It reads text from stdin, replaces $VAR and ${VAR} patterns with their values from the environment, and writes the result to stdout.
This tool is essential for generating configuration files from templates in deployment pipelines and containerized environments. A template might contain ${DATABASE_HOST} which gets replaced with the actual value at deployment time.
When called without a variable list, envsubst replaces all environment variable references. To substitute only specific variables (leaving others as literal text), provide a SHELL-FORMAT argument listing the desired variables.
The tool handles both $VAR and ${VAR} syntax. Missing variables are replaced with empty strings. The ${VAR:-default} syntax for defaults is NOT supported; envsubst performs simple substitution only.
Common uses include Docker entrypoint scripts that configure services at runtime, CI/CD pipelines generating configs, and any scenario where configuration needs to adapt to the runtime environment.
PARAMETERS
-v, --variables
Output the variables occurring in SHELL-FORMAT or stdin.-V, --version
Display version information.-h, --help
Display help information.SHELL-FORMAT
Space-separated list of variables to substitute. If specified, only these variables are replaced; others remain as literal text.
CAVEATS
Does not support shell parameter expansion features like ${VAR:-default} or ${VAR:+value}. All variables must exist in the environment; missing ones become empty strings. Dollar signs that should remain literal need escaping as $$. Part of gettext package.
HISTORY
envsubst is part of GNU gettext, the internationalization toolkit. While gettext is primarily for software translation, envsubst became widely adopted as a general-purpose template tool, particularly in the DevOps community for configuration management. It's available on virtually all Unix-like systems as part of the gettext-base package.
