I'm trying to perform environment variable replacement through envsubst, but I want to only replace specific variables.

From the docs I should be able to tell envsubst to only replace certain variables but I'm failing to be able to do that.

For example, if I have a file containing:


how should I execute envsubst so that it only replaces the reference to ${VAR_1}?


Before calling envsubst you should use export using single quotes to get back VAR_1 modified. As in:

export VAR_1='somevalue'

For more details, please see:

How to substitute shell variables in complex text files

  • 43
    Correct answer is below – Craig Mar 19 '18 at 17:40
  • 1
    @JoãoAngelo Would you clarify why this was marked has the correct one over the other answer? For me it seems more appropriate asking the original asker. – Rui F Ribeiro Jan 22 '20 at 11:20
  • @RuiFRibeiro your answer will replace all variables (not only specific variables). all patterns like $varname will be evaluated (not only the variables you're targetting). – MhdSyrwan Feb 26 '20 at 12:31
  • check this case, serverfault.com/questions/577370/… – MhdSyrwan Feb 26 '20 at 12:32

Per the man page:


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:


and some values like

export  VAR1="one" VAR2="two" VAR3="three"

then running

envsubst '${VAR1} ${VAR3}' <infile


envsubst '${VAR1},${VAR3}' <infile


envsubst '${VAR1}
${VAR3}' <infile



Or, if you prefer backslash:

envsubst \$VAR1,\$VAR2 <infile


  • 29
    why is this not the accepted answer? – Qsiris Jan 11 '18 at 13:39
  • 8
    Yes this is the right answer! – Thomas Decaux Feb 26 '18 at 9:31
  • 8
    If you know a variable prefix to use to limit the variables that can be substituted: envsubst "$(printf '${%s} ' ${!PREFIX*})" < infile – Sam Liddicott Jan 22 '19 at 15:13
  • 5
    Or if you have a list of them in SUBST_VARS then: envsubst "$(printf '${%s} ' $SUBST_VARS)" < infile – Sam Liddicott Jan 22 '19 at 15:15
  • 1
    This answer ALSO substitutes an empty string for variables present that are not set. As opposed to ONLY replacing the specific variables. – Chris Stryczynski Mar 6 '20 at 12:45

Although related to docker, the utility envplate should do the job https://github.com/kreuzwerker/envplate

From the readme:

Trivial templating for configuration files using environment keys. References to such keys are declared in arbitrary config files either as:

${key} or

${key:-default value}

gnutext's envsubst only replaces ${key}; if missing is replaced by ''.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.