envsubst: The simplest bash variable substitution tool

Shahin HemmatiShahin Hemmati
1 min read

๐Ÿง ๐—ช๐—ต๐—ฎ๐˜ ๐—ถ๐˜€ ๐—ฒ๐—ป๐˜ƒ๐˜€๐˜‚๐—ฏ๐˜€๐˜?
envsubst (GNU gettext) streams stdin โžœ stdout, replacing any $VAR with their environment values. Think sed for env-vars, no regex.

๐Ÿš€ ๐—ช๐—ต๐—ฒ๐—ป ๐—ฑ๐—ผ๐—ฒ๐˜€ ๐—ถ๐˜ ๐˜€๐—ต๐—ถ๐—ป๐—ฒ?
โ€ข Container start-up configs: inject secrets or hostnames into nginx.conf
โ€ข Quick local scripting: stamp todayโ€™s date or $HOME into a file in 2 lines
โ€ข CI/CD pipelines: render YAML/INI/JSON per-environment without Helm/Jinja

๐Ÿ‘จโ€๐Ÿ’ป ๐—–๐—ผ๐—ฑ๐—ฒ:

export IP=$(curl -s https://api.ipify.org)
envsubst '$IP' < hostname.tmpl > hostname.conf

โ„น๏ธ ๐—˜๐˜…๐—ฝ๐—น๐—ฎ๐—ป๐—ฎ๐˜๐—ถ๐—ผ๐—ป:

1๏ธโƒฃ curl fetches my public IP address and stores it in the IP environment variable.

2๏ธโƒฃ envsubst reads the template file, replaces every instance of $IP with that value, and saves the finished file as hostname.conf.

๐ŸŽž๏ธ Check the 14-second demo below to watch it happen in real time.

0
Subscribe to my newsletter

Read articles from Shahin Hemmati directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shahin Hemmati
Shahin Hemmati

I'm a curious Geek with an insatiable thirst to learn new technologies and enjoy the process every day. I aim to deliver high-quality services with the highest standards and cutting-edge DevOps technologies to make people's lives easier.