Hi š,
Introduction
In this short article I want to showcase a nice and useful Linux command.
The comand š„ envsubst š„. In short it Substitutes the values of environment variables.
Itās great for populating configuration files with values from environment variables, a common operation for developers containerizing their applications.
Example Usecase
Letās say we are containerizing an application and we have the following file configuration.yaml, and we want to modify the values of the environment field and the log_level field without adding the additional complexity of mounting the configuration.yaml file into the container/pod.
configuration:
server: "the-app"
environment: "production"
log_level": "info"
To change the values of the environment and loglevel fields we create a configuration-template.yaml like so:
configuration:
server: "the-app"
environment: "$ENV_ENVIRONMENT"
log_level": "$ENV_LOGLEVEL"
Then we run eventsubt to substitute the configuration values:
export ENV_ENVIRONMENT=stagging
export ENV_LOGLEVEL=trace
envsubst < configuration-template.yaml > configuration.yaml
The resulting configuration.yaml file will contain:
configuration:
server: "the-app"
environment: "stagging"
log_level": "trace"%
Conclusion
The envsubst command enables us to write configuration files with placeholders that will be replaced with actual values from the environment variables. Before being aware if this command I always thought that I have to somehow parse the files, substitute variables dynamically, ensure that the substitution didnāt change the file format or break something. Now I just envsubst .
Thanks for reading and happy dev-ing! š„ļøš§āš»
envsubst help
Usage: envsubst [OPTION] [SHELL-FORMAT]
Substitutes the values of environment variables.
Operation mode:
-v, āvariables output the variables occurring in SHELL-FORMAT
Informative output:
-h, āhelp display this help and exit
-V, āversion output version information and exit
In normal operation mode, standard input is copied to standard output,
with references to environment variables of the form $VARIABLE or ${VARIABLE}
being replaced with the corresponding values. If a SHELL-FORMAT is given,
only those environment variables that are referenced in SHELL-FORMAT are
substituted; otherwise all environment variables references occurring in
standard input are substituted.
When āvariables is used, standard input is ignored, and the output consists
of the environment variables that are referenced in SHELL-FORMAT, one per line.
Report bugs in the bug tracker at
or by email to .