envsubst

2022-10-28 ยท 1 min read

  • Substitute environment variables ($VARS) in stdin with current env values and output to stdout.

examples #

# envsubst
#
# Substitutes environment variables with their value in shell format strings.
# Variables to be replaced should be in either `${var}` or `$var` format.
#
# More information: <https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html>.

# Replace environment variables in stdin and output to stdout:
$ echo 'I live at $HOME.' | envsubst
I live at /home/phlip9.

# Replace environment variables in an input file and output to stdout:
$ cat path/to/input_file
I live at $HOME.
My name is $USERNAME.

$ envsubst < path/to/input_file
I live at /home/phlip9.
My name is phlip9.

# Replace only some environment variables from a space-separated list:
$ envsubst '$USERNAME' < path/to/input_file
I live at $HOME.
My name is phlip9.