How to use the command 'envsubst' (with examples)

How to use the command 'envsubst' (with examples)

The ’envsubst’ command is a useful tool for substituting environment variables with their values in shell format strings. This allows for the easy replacement of variables in various contexts, such as input files or standard input. This article will provide examples of how to use the ’envsubst’ command in different use cases.

Use case 1: Replace environment variables in stdin and output to stdout

Code:

echo '$HOME' | envsubst

Motivation: This use case is useful when you want to substitute environment variables in a string before using it as standard input. It can be handy for providing dynamic values in scripts or configurations.

Explanation:

  • echo '$HOME': This command is used to print the value of the ‘HOME’ environment variable. It is enclosed in single quotes to prevent the shell from expanding it.
  • |: The pipe operator is used to redirect the output of the ’echo’ command as the input to the ’envsubst’ command.
  • envsubst: The ’envsubst’ command reads the input from ‘stdin’ and substitutes any environment variables present in the input with their corresponding values. The result is then printed to ‘stdout’.

Example output: If the value of the ‘HOME’ environment variable is ‘/home/user’, the output of this command will be ‘/home/user’.

Use case 2: Replace environment variables in an input file and output to stdout

Code:

envsubst < path/to/input_file

Motivation: This use case is useful when you have an input file that contains shell format strings with environment variables, and you want to replace those variables with their values. The resulting output can then be used as desired.

Explanation:

  • envsubst < path/to/input_file: The ‘<’ symbol is used to redirect the contents of the ‘path/to/input_file’ as the input to the ’envsubst’ command. The ’envsubst’ command then substitutes any environment variables present in the input file with their corresponding values. The result is printed to ‘stdout’.

Example output: If the ‘path/to/input_file’ contains the string ‘$USER is logged in.’, and the value of the ‘USER’ environment variable is ‘john’, the output of this command will be ‘john is logged in.’.

Use case 3: Replace environment variables in an input file and output to a file

Code:

envsubst < path/to/input_file > path/to/output_file

Motivation: This use case is useful when you have an input file with shell format strings that contain environment variables, and you want to replace those variables with their values. The resulting output can be redirected to a file for further use or for creating modified versions of the input file.

Explanation:

  • envsubst < path/to/input_file: The ‘<’ symbol is used to redirect the contents of the ‘path/to/input_file’ as the input to the ’envsubst’ command. The ’envsubst’ command then substitutes any environment variables present in the input file with their corresponding values.
  • > path/to/output_file: The ‘>’ symbol is used to redirect the output of the ’envsubst’ command to the ‘path/to/output_file’. The resulting output, with environment variables substituted, is saved to the specified file.

Example output: If the ‘path/to/input_file’ contains the string ‘Welcome, $USER!’, and the value of the ‘USER’ environment variable is ‘john’, the contents of the ‘path/to/output_file’ will be ‘Welcome, john!’.

Use case 4: Replace environment variables in an input file from a space-separated list

Code:

envsubst '$USER $SHELL $HOME' < path/to/input_file

Motivation: This use case is useful when you want to substitute multiple environment variables in an input file from a space-separated list. It allows for more control over which variables to substitute and can be beneficial in scenarios where you want to selectively replace specific variables.

Explanation:

  • envsubst '$USER $SHELL $HOME': The arguments following the ’envsubst’ command specify a space-separated list of environment variables to substitute. In this case, the ‘USER’, ‘SHELL’, and ‘HOME’ variables will be replaced with their corresponding values.
  • < path/to/input_file: The ‘<’ symbol is used to redirect the contents of the ‘path/to/input_file’ as the input to the ’envsubst’ command. The ’envsubst’ command then substitutes the specified environment variables present in the input file with their corresponding values.

Example output: If the ‘path/to/input_file’ contains the string ‘$USER uses $SHELL at $HOME’, and the values of the ‘USER’, ‘SHELL’, and ‘HOME’ environment variables are ‘john’, ‘/bin/bash’, and ‘/home/user’ respectively, the output of this command will be ‘john uses /bin/bash at /home/user’.

Conclusion:

The ’envsubst’ command is a versatile tool for substituting environment variables with their values in shell format strings. It can be used in various scenarios to provide dynamic values and modify input files. By understanding and utilizing the different use cases of ’envsubst’, you can enhance your scripting and configuration workflow.

Related Posts

How to use the command "bcomps" (with examples)

How to use the command "bcomps" (with examples)

The “bcomps” command is a part of the Graphviz suite of tools and is used to decompose graphs into their biconnected components.

Read More
How to use the command shnsplit (with examples)

How to use the command shnsplit (with examples)

Shnsplit is a command-line tool that can split audio files according to a .

Read More
How to use the command 'snyk' (with examples)

How to use the command 'snyk' (with examples)

Description: The ‘snyk’ command is used to find vulnerabilities in your code and remediate risks.

Read More