envsubst substitutes environment variables in a shell string or script. When used with no options, copies stdin to stdout, replacing any environment variable string, such as $VARIABLE or ${VARIABLE}, with the appropriate environment variable value. So, “My editor is $EDITOR” would be converted to “My editor is /usr/bin/emacs.” Specifying a shell format limits the substitutions to those variables referenced in the shell format.
envsubst command line options
envsubst command has very few options as shown below:
Option | Description |
---|---|
-h, –help | Print help message and then exit. |
-v, –variables | Display the variables referenced in the shell format, and then exit. |
-V, –version | Print version information and then exit. |
If you get an error as shown below while running the envsubst command:
envsubst: command not found
you may try installing the gettext package as per your choice of distribution.
Distribution | Command |
---|---|
OS X | brew install gettext |
Debian | apt-get install gettext-base |
Ubuntu | apt-get install gettext-base |
Alpine | apk add gettext |
Arch Linux | pacman -S gettext |
Kali Linux | apt-get install gettext-base |
CentOS | yum install gettext |
Fedora | dnf install gettext |
Raspbian | apt-get install gettext-base |
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 < path/to/input_file
3. Replace environment variables in an input file and output to a file:
$ envsubst < path/to/input_file > path/to/output_file
4. Replace environment variables in an input file from a space-separated list:
$ envsubst '$USER $SHELL $HOME' < path/to/input_file