Linux command
envsubst 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Substitute environment variables
envsubst < [template.txt]
Substitute and save
envsubst < [template.txt] > [output.txt]
Substitute only specific variables
envsubst '$USER $HOME' < [template.txt]
Substitute from piped input
echo 'Hello $USER' | envsubst
List all referenced variables
envsubst --variables < [template.txt]
Substitute in a config template
cat [nginx.conf.template] | envsubst '$SERVER_NAME $PORT' > [nginx.conf]
说明
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.
参数
- -v, --variables
- Output the variables occurring in SHELL-FORMAT or stdin.
- -V, --version
- Display version information.
- -h, --help
- Display help information.
FAQ
What is the envsubst command used for?
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.
How do I run a basic envsubst example?
Run `envsubst < [template.txt]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -v, --variables do in envsubst?
Output the variables occurring in SHELL-FORMAT or stdin.