Skip to content

Instantly share code, notes, and snippets.

@MikeGarde
Created November 17, 2023 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeGarde/58c3ab99d4898b8791a49c940f2f51a4 to your computer and use it in GitHub Desktop.
Save MikeGarde/58c3ab99d4898b8791a49c940f2f51a4 to your computer and use it in GitHub Desktop.
Merge ENV Files
#!/bin/bash
# Function to load .env file and override any duplicates
load_env() {
local env_file=$1
if [ -f $env_file ]; then
while IFS= read -r line
do
if grep -q "$(echo $line | cut -d'=' -f1)" .env; then
# If key exists in .env, remove it
sed -i "/$(echo $line | cut -d'=' -f1)/d" .env
fi
# Add the key-value pair from the .env file
echo "$line" >> .env
done < $env_file
fi
}
# Create a new .env file
> ../.env
# Load .env.testing.ci - this is our base .env file for testing
if [ -f ../.env.testing.ci ]; then
while IFS= read -r line
do
echo "$line" >> .env
done < ../.env.testing.ci
fi
# Load .env.testing.local and override any duplicates
load_env ../.env.testing.local
load_env ../.env.testing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment