How to use envsubst command to replace environment variables in Linux system?

In Linux systems, environment variables are a very common mechanism, and they are used to store important system information, such as user login names, paths, and so on. When you need to use these variables in the script, you can use the envsubst command, which can replace the value of the environment variable into a text file.

This article will explain how to use the envsubst command to substitute environment variables.

Definition of environment variables

In the Linux system, environment variables can be defined through the export command, as follows:

export MY_VAR=my_value

In the above example, MY_VAR is set to my_value, which can be used in shell scripts and other commands.

Use envsubst to replace environment variables

Now, let's say we have a file that contains some values ​​that need to be substituted into environment variables. This file can be a template file, which contains some tags, such as ${MY_VAR}, which will be replaced with the actual environment variable values.

We can use the envsubst command to substitute the value of an environment variable into a file. For example, we have the following files:

Hello ${USER}, welcome to ${HOME} directory.

We can use the following command to combine USER and {USER} withReplace USER and { HOME} with actual environment variable values:

envsubst < file.txt > newfile.txt

After executing the above command, the new file newfile.txt will contain the following content:

Hello username, welcome to /home/username directory.

Note: Before executing the envsubst command, make sure that all environment variables that need to be replaced have been defined.

Use multiple environment variable substitutions

When we need to use multiple environment variable substitutions, we can put them all in a bracket and use $ to refer to them, for example:

export MY_NAME="John"
export MY_AGE="30"
envsubst < file.txt > newfile.txt

If file.txt contains the following:

My name is $MY_NAME and I am $MY_AGE years old.

then newfile.txt will contain the following:

My name is John and I am 30 years old.

Using the envsubst command in a script

It is also very simple to use the envsubst command in the script, just save the text to be replaced in a variable, and then use the envsubst command to replace the value in the variable into the target file.

Here is a simple example:

#!/bin/bash

MY_NAME="John"
MY_AGE="30"

text="My name is \$MY_NAME and I am \$MY_AGE years old."
echo $text | envsubst > newfile.txt

After executing the above script, a new file newfile.txt will be created in the current directory, which contains the following content:

My name is John and I am 30 years old.

in conclusion

Use the envsubst command to use the envsubst command to easily replace the value of an environment variable into a text file, which is very useful for writing scripts and configuration files. However, it should be noted that when using the envsubst command, make sure that all environment variables that need to be replaced have been defined, otherwise the replacement result may be incorrect.

In addition to the envsubst command, there are some other commands that can be used to process environment variables, such as:

  • The echo command: can be used to output the value of an environment variable.
  • printenv command: can be used to print the value of all environment variables.
  • set command: It can be used to set and view variables and environment variables in the shell.

In general, mastering the use and management of environment variables is very important for the management and programming of Linux systems.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/130144450