Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Using envsubst to Merge Environment Variables into Config Files


This command is available on Linux and macOC by default and it lets you template out files with ENV vars to create dynamic config files.

Quick Jump: Demo Video

Let’s say you have an nginx or Kubernetes config file which doesn’t support templating out of the box and you want to dynamically create config files based on 1 or more environment variables. This is what envsubst lets you do.

This can help you avoid creating 2 separate config files that are nearly identical. Instead you can define what you want to be different as environment variables and send it to envsubst to be processed. We’ll go over a few examples below:

Demo Video


Trying out envsubst on the command line:

$ export PERSON=nick
$ echo 'hello ${PERSON}' | envsubst
hello nick

# We're using single quotes above instead of double quotes to avoid the variable
# being interpreted by your shell. Single quotes will ensure it's picked up
# and processed by envsubst.

Piping the output of envsubst as input to another command:

$ source .env
$ envsubst < aws-cluster-tpl.yaml | eksctl create cluster --config-file - --dry-run

Outputting a new config file with the results of running it through envsubst:

$ source .env
$ envsubst < aws-cluster-tpl.yml > aws-cluster-example.yaml


  • 0:12 – envsubst lets you generate templated files with ENV variables
  • 0:44 – Trying out envsubst on the command line with a simple echo
  • 1:19 – Going over the use case of templating out an eksctl config file
  • 2:29 – Taking a look at a templated eksctl config file with ENV vars
  • 3:34 – Running the templated config through envsubst
  • 4:53 – Exporting the processed config to eksctl as input
  • 6:29 – Writing out a new processed config file based on using envsubst
  • 7:00 – Being able to define default values using a fork of envsubst

What was the last thing you used envsubst for? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per month (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.