The “envsubst” command-line tool is used to substitute environment variables with their corresponding values in shell format strings. It allows you to dynamically replace variables in a text file or a shell script with the actual values of those variables, enabling dynamic configuration and parameterization of scripts or templates.
Here’s a more detailed explanation of the “envsubst” command-line tool and its key features:
- Environment Variable Substitution: The primary purpose of “envsubst” is to perform environment variable substitution. It scans the input file or text and replaces occurrences of variables with their corresponding values from the environment.
- Shell Format Strings: “envsubst” supports shell format strings, which means that variables to be replaced should be in either ${var} or $var format. These variables are typically defined in the environment or provided as command-line arguments.
- Dynamic Configuration: By using “envsubst” in shell scripts or configuration files, you can create dynamic configurations that adapt to the environment where they are executed. This allows you to parameterize your scripts or templates, making them more flexible and reusable.
- Input and Output: “envsubst” takes an input file or reads from standard input and performs variable substitution. The result is then written to standard output or redirected to an output file. This makes it easy to integrate “envsubst” into existing shell scripts or pipeline it with other command-line tools.
- Recursive Substitution: “envsubst” performs recursive substitution, meaning that it replaces variables that contain other variables. For example, if a variable VAR1 is defined as Hello and another variable VAR2 is defined as ${VAR1}, World!, “envsubst” will substitute ${VAR1} with Hello and ${VAR2} with Hello, World!.
- Default Values: “envsubst” allows you to specify default values for variables that are not set in the environment. If a variable is not defined, it will be replaced with the provided default value, if any. This feature ensures that your scripts or templates gracefully handle cases where certain variables are not available.
- Batch Mode: “envsubst” supports a batch mode where it can substitute multiple variables at once. This is useful when you have a large number of variables to replace in a given input file or text.
- Cross-Platform Compatibility: “envsubst” is available on most Unix-like systems, including Linux, macOS, and BSD variants. This ensures cross-platform compatibility and allows you to use it in different environments.
“envsubst” simplifies the process of substituting environment variables with their values in shell format strings. By dynamically replacing variables, you can create more flexible and configurable scripts, configuration files, or templates that adapt to the environment in which they are executed.
envsubst Command Examples
1. Replace environment variables in stdin and output to stdout:
# echo '$HOME' | envsubst
2. Replace environment variables in an input file and output to stdout:
# envsubst
3. Replace environment variables in an input file and output to a file:
# envsubst /path/to/output_file
4. Replace environment variables in an input file from a space-separated list:
# envsubst '$USER $SHELL $HOME'