Wednesday, October 9, 2024

BASH Tutorial: How to Use Envsubst to Replace Environment Variables

 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.

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=jr@mail.com
$ 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.

$ envsubst < myfile
    

Output:

      Username is: your_username
Email is: jr@mail.com
Password is: changeme
    

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.

      $ export USER=jake
    

Output:

      Username is: jake
Email is: jr@mail.com
Password is: changeme
    

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:

      Username is: 
Email is: 
Password is:
    

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.

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:

      Username is: your_username
Email is: jr@mail.com
Password is: changeme
    

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:

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

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:

      Username is: $USER
Email is: jr@mail.com
Password is: changeme
    

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:

      Username is: $USER
Email is: jr@mail.com
Password is: changeme
    

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.

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.

0 comments:

Post a Comment