Replacing only specific variables with envsubst
Per the man page:
envsubst [OPTION] [SHELL-FORMAT]
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.
Where SHELL-FORMAT strings are "strings with references to shell variables in the form $variable
or ${variable}
[...] The variable names must consist solely of alphanumeric or underscore ASCII characters, not start with a digit and be nonempty; otherwise such a variable reference is ignored.".
So, one has to pass the respective variables names to envsubst
in a shell format string (obviously, they need to be escaped/quoted so as to be passed literally to envsubst
). Example:
input file e.g. infile
:
VAR1=${VAR1}
VAR2=${VAR2}
VAR3=${VAR3}
and some values like
export VAR1="one" VAR2="two" VAR3="three"
then running
envsubst '${VAR1} ${VAR3}' <infile
or
envsubst '${VAR1},${VAR3}' <infile
or
envsubst '${VAR1}
${VAR3}' <infile
outputs
VAR1=one
VAR2=${VAR2}
VAR3=three
Or, if you prefer backslash:
envsubst \$VAR1,\$VAR2 <infile
produces
VAR1=one
VAR2=two
VAR3=${VAR3}