Master the Linux ‘envsubst’ Command: A Comprehensive Guide

Peter Hou
2 min readMay 5, 2023

--

This comprehensive guide to the envsubst command in Linux provides an overview of its history, usage, and common parameters, along with advanced techniques and important considerations. As a utility for substituting environment variable references in text files or strings, envsubst streamlines configuration management and scripting tasks. By understanding how to use this versatile tool and its various features, users can more effectively manage environment-specific settings in a consistent and maintainable way.

Instructions

This article will provide detailed instructions on the usage of the envsubst command in Linux, including its history, common use cases, and some advanced techniques.

History

The envsubst command is a part of the GNU gettext package, which was first released in 1995. It is a utility designed to substitute environment variable references in a given text file or string.

When and why to use it

The envsubst command is best used for substituting environment variables in text files or strings, especially in configuration files or scripts that require dynamic values. This command can help streamline the process of managing environment-specific settings in a consistent and maintainable way.

How to use it

To use envsubst, provide the input either as a file or a string, and the command will replace any environment variable references with their corresponding values.

echo '${USER} lives in ${HOME}' | envsubst

The commonly used parameters

  • -v, --variables: List all recognized variable names
envsubst --variables < input-file
  • -i, --input: Read input from a specified file instead of standard input
envsubst -i input-file
  • -o, --output: Write output to a specified file instead of standard output
envsubst -i input-file -o output-file
  • --no-symlink: Do not follow symbolic links when processing input files
envsubst --no-symlink -i input-file
  • --no-unset: Keep references to unset variables in the output
echo '${UNSET_VAR} is unset' | envsubst --no-unset

Other supported parameters

There are no other parameters supported by envsubst.

Most common use cases

  • Substituting environment variables in configuration files
envsubst < input.conf.template > output.conf
  • Generating scripts with dynamic values
envsubst < input.sh.template > [output.sh](<http://output.sh/>)
chmod +x [output.sh](<http://output.sh/>)

The tricky skills

  • Using envsubst with a custom set of variables:
VAR1=value1 VAR2=value2 envsubst '${VAR1} ${VAR2}' <<< 'This is ${VAR1} and this is ${VAR2}'

What needs to be noted

  • Be careful when using envsubst with user-provided input, as it may lead to unintended variable substitutions or security risks.
  • Remember that envsubst only works with POSIX-style environment variable references (i.e., ${VARNAME}), not with shell parameter expansions (e.g., ${VARNAME:-default}).

Conclusion

The envsubst command is a powerful and versatile tool for managing environment variable substitutions in text files and strings. By understanding its usage, parameters, and common use cases, you can simplify your configuration management and scripting tasks in Linux.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

No responses yet