Skip to main content

Set Environment Variables in Your Bash Shell From a .env File

Published: 2020-07-28 • Last updated: 2021-01-14

I experimented with many ways of doing this, but my favorite solution was from this stackoverflow answer. It keeps the .env file syntax clean so that it can be used across projects. Here’s a minimal, reproducible example.

Also see Version 2 of this post.

# .env file

# Some comment

# script

#!/usr/bin/env bash

# Show env vars
grep -v '^#' .env

# Export env vars
export $(grep -v '^#' .env | xargs)

# Usage

  1. Set env vars in your Bash shell

  2. Confirm env vars

     echo "${FIRST_NAME} ${LAST_NAME}"