How to Use Envsubst to Replace Environment Variables in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

At one point, you may have come across a template, configuration, or initialization file containing bash variables as placeholders that you’ll need to fill in before actual usage.

To fill those variables, you can either use the globally set environment variables, such as β€œ$HOMEβ€œ, β€œ$USERβ€œ, etc., or if they require something that doesn’t come under the global variable, you need to first export them before use.

Some variables (like email or password) are too sensitive to export globally in your system. Thus, in this article, I’ll show how to temporarily export environment variables and then replace the placeholder variables using the β€œenvsubst” command.

Tutorial Details

DescriptionEnvsubst command
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Internet RequiredNo

How to Use Envsubst to Replace Environment Variables

Envsubst is a built-in command-line tool used to search for variable patterns (such as β€œ$VARIABLE” or β€œ[$VARIABLE]β€œ) and replace them with existing global variables if their names match. Alternatively, you can temporarily export variables in your current shell session and use them to substitute the variables mentioned in the file.

To showcase the usage of the β€œenvsubst” command, I’ll use a file called β€œmyfileβ€œ, which includes:

Username is: $USER
Email is: $EMAIL
Password is: $PASSWD

In this file, we have three variables, among which the first β€œ$USER” is also a global variable, while β€œ$EMAIL” and β€œ$PASSWD” require manual specification.

So, first, I’ll temporarily export the β€œ$EMAIL” and β€œ$PASSWORD” variables using the following commands:

$ export [email protected]
$ export PASSWD=changeme

Once you’ve finished exporting the variables, use the β€œenvsubst” command to replace the file variables with both globally and temporarily exported variables.

Make sure to replace β€œmyfile” with the actual filename.
$ envsubst < myfile


replacing env variable using envsubst

As you can see, the variables mentioned in the file are effectively substituted. If you wish to replace the β€œ$USER” variable in the chosen file other than the global variable, you can export a new variable with that name.

These changes are temporary and won’t affect your global variables.
$ export USER=jake


change global variable value with temporary using envsubst

You can also unset global or exported variables using the unset command. For example, with the following command, I’ve unset the values of all three variables.


Now, if you run the β€œenvsubst” command again, it will result in a variable placeholder with blank spaces:

remove variable with envsubt command

If you’re wondering if β€œ$USER” is a global variable that’s later replaced with a temporary one, then, after unsetting, why doesn’t it display the global variable value? You might be a new Linux user, so to put it simply, exporting a new variable replaces the existing one rather than creating a new one, so unsetting it removes it entirely.

Redirect the Output to a New File

Once you’ve made all the adjustments to your file by using or exporting a new variable, you can redirect (or save) your output to a new file. For that purpose, you can use the β€œ>” redirection symbol and specify the new file name.

For example, executing this command will redirect the output to a file called β€œnewfileβ€œ:

$ envsubst < myfile > newfile


redirect the envsubst output to newfile

Replace Specific Variables Using the Envsubst With SHELL-FORMAT

The β€œSHELL-FORMAT” argument allows you to select a variable for substitution. This way, you can specifically select single or multiple global or exported variables that need to be replaced in the file while leaving the rest unchanged.

The syntax for specifying the β€œSHELL-FORMAT” argument is quite simple and flexible, where all you need to do is specify the variable within the β€œ''” (single quotes), as shown.

# The following is simple syntax to use the SHELL-FORMAT argument:
$ envsubst '$variable' < file

# The following is the syntax to allow multiple variables to be replaced:
$ envsubst '$variable1 $variable2 $variable3' < file

# The following syntax replaces a variable with message for a hint:
$ envsubst 'subsituting the $variable1 and $variable2' < file

To showcase its usage, I’ll perform all three methods mentioned above in β€œmyfileβ€œ, starting with allowing only a single β€œ$USER” variable for substitution.

$ envsubst '$USER' < myfile


substituting one variable using envsubst

You can see in the above image that only the specified variable was substituted. Now, let’s replace multiple variables like β€œ$EMAIL” and β€œ$PASSWORD” using the following command:

$ envsubst '$EMAIL, $PASSWD' < myfile


substituting multiple variable using envsubst

Finally, rather than only mentioning the variables, you can also write a message within the β€œSHELL-FORMAT” argument, as shown:

$ envsubst 'Replacing $EMAIL and $PASSWD but later remove them' < myfile


writing message in shell-format

Notice how easy it is to specifically replace single or multiple variables in the file. You can specify your variable with or without a β€œ,” (comma) as a separator in the β€œSHELL-FORMAT” argument.

If you’re planning to use β€œenvsubst” in a shell script, you can write a message using the above method. This won’t affect the functionality, but it will assist the administrator in its use case.

Finally, this article comes to an end.

The β€œenvsubst” command is very useful for substituting single or multiple variable values in a file. We explored various methods of using it, but if anything was overlooked, then do inform us in the comments.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.