#!/usr/bin/env bash load_env_defaults() { local env_file="${1:-.env}" if [[ ! -f "$env_file" ]]; then return 0 fi while IFS= read -r line || [[ -n "$line" ]]; do if [[ "$line" =~ ^[[:space:]]*$ ]] || [[ "$line" =~ ^[[:space:]]*# ]]; then continue fi if [[ "$line" != *=* ]]; then continue fi local key="${line%%=*}" local value="${line#*=}" key="${key#"${key%%[![:space:]]*}"}" key="${key%"${key##*[![:space:]]}"}" value="${value#"${value%%[![:space:]]*}"}" value="${value%"${value##*[![:space:]]}"}" if [[ ! "$key" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then continue fi if [[ -n "${!key+x}" ]]; then continue fi if [[ "$value" =~ ^\"(.*)\"$ ]]; then value="${BASH_REMATCH[1]}" elif [[ "$value" =~ ^\'(.*)\'$ ]]; then value="${BASH_REMATCH[1]}" fi export "$key=$value" done < "$env_file" }