Skip to main content
Commands

How to Replace Environment Variables Using the envsubst Command

Learn how to replace environment variables using the envsubst command. This guide will show you how with examples and tips.

Sagar Sharma

Warp Terminal

While working with scripts, most of the users are going to use environment variables but sometimes they use variables that come at a risky cost.

So if you want to replace environment variables, you can use the envsubst command.

Replace environment variables using the envsubst command on Linux

The envsubst command is used to get a substitute of environment variables and that's what its name suggests.

But it won't change your variables directory. First, it will look for variable patterns (such as $VARIABLE or [$VARIABLE]).

And then it will replace the found variables with a specified bash variable.

💡
The envsubst command will only recognize exported variables.

To replace your environment variables using envsubst, you will have to follow the given command structure:

envsubst [OPTION] [SHELL-FORMAT]

Now, let's have a look at how you can change the environment variable.

For that purpose, I will be using a file named confidential.txt containing:

A sample file containg password and username!

And should not be shared by any means.

My loging credentials are:

        username=$USERNAME
        password=$PASSWORD

Now, to have substitute values for both $USERNAME and $PASSWORD, First, I will create an exported variable for both of them:

export USERNAME=abhiman
export PASSWORD=strongphrase
export environment variables using the export command in linux

Once you are done with exporting variable values, you can invoke the envsubst command for the file you've created:

envsubst < confidential.txt
use envsubst command to change environment variable values in file on linux

And as you can see, the values have been altered successfully!!

Similarly, you can unset those variables using the unset command. Let me show you how.

First, unset the variable that you specified with the export command.

In my case, those were, USERNAME and PASSWORD:

unset USERNAME PASSWORD

Now, if you run the envsubst command again, it will result in blank spaces:

unset variables in linux

And if you are curious about how it happened, let me remind you of something that I mentioned earlier.

The envsubst command will only work with exported variables and when I used the unset command, the values were null.

And when the envsubst command finds no value to replace, it will result in blank spaces.

Redirect output to the specific file

Having an output directory to the terminal is not always the best way of doing things and in that case, you can redirect the standard output to the file.

To redirect the output to the file, you will have to use the > redirection symbol.

For example, here, I will be redirecting the output to the file named Output.txt:

envsubst < confidential.txt > Output.txt
redirect envsubst output to file on linux

Replace specific variables using the envsubst command with SHELL-FORMAT

So let's suppose you have exported multiple environment variables but you only want to substitute a few of them.

And in that case, you can use SHELL-FORMAT.

The syntax is quite flexible where all you need to do is specify the variable in '' and you can use it in a variety of ways:

It can be used like:

envsubst '$variable' > file

Or you can append multiple variables like this:

envsubst '$variable1 $variable1 $variable3' > file

And you can even append basic text for better understanding:

envsubst 'subsitute the $variable1 and $variable2' > file

Quite flexible. Right?

Now, for this example, I will be using a file named Substitute.txt which contains the following:

Hello, My name is $USER.

And these are login credentials for $SERVICE:

        username=$USERNAME
        password=$PASSWORD

Not meant for public use!

Next, I will export values for each variable used in the above file:

export USER=sagar export SERVICE=AWS export USERNAME=LHB export PASSWORD=randomphrase  

And without replacing any specific variable, it should get me the following output:

use envsubst command to substitute environment variables

So let's say, I only want the values of $USER and $SERVICE to be reflected in the output, so I will be using the following:

envsubst '$USER $SERVICE' < Substitute.txt
substitue specific variables using envsubst with SHELL-FORMAT on linux

And as you can see, it showed the values of $USER and $SERVICE while left the $USERNAME and $PASSWORD as it is.

A neat way to handle private info. Isn't it?

Love to play with variables? We have more

If you are a developer and love to use environment variables, here's how you can know the value of each one:

How to Print Environment Variables in Linux
Wondering what environment variables are set on your current shell? Learn how to print environment variables in Linux.

Or if you are keen to know unusual ways to use variables in bash, here's how you do it:

Use Variables in Bash Scripts [Unusual Advanced Ways]
You might have used variables in Bash before, but probably not like this.

I hope you will find this article helpful and if you have any queries related to this or any other guide, or just want me to cover any specific topic, let me know in the comments.

Sagar Sharma