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.
Prerequisites
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

Output:

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

Output:

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.

$ unset USER EMAIL PASSWD

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

Output:

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

Output:

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

Output:

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

Output:

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.