envsubst: The simplest bash variable substitution tool


๐ง ๐ช๐ต๐ฎ๐ ๐ถ๐ ๐ฒ๐ป๐๐๐๐ฏ๐๐?
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.
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.