Skip to content

Commit c6e1034

Browse files
committed
Create load-env.sh
1 parent 522a90d commit c6e1034

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

load-env.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
# Load variables from the .env file into the current shell.
4+
# NOTE: This script must be *sourced*, not executed, e.g.:
5+
# source load-env.sh
6+
# or
7+
# . ./load-env.sh
8+
9+
# If this script is run directly (./load-env.sh), print a warning because
10+
# environment changes will not persist in the parent shell.
11+
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
12+
echo "[load-env] This script must be sourced to affect the current shell:" >&2
13+
echo " source load-env.sh" >&2
14+
exit 1
15+
fi
16+
17+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
18+
ENV_FILE="$SCRIPT_DIR/.env"
19+
20+
if [[ ! -f "$ENV_FILE" ]]; then
21+
echo "[load-env] No .env file found at: $ENV_FILE" >&2
22+
return 1
23+
fi
24+
25+
# Auto-export all variables defined while the script runs
26+
set -a
27+
source "$ENV_FILE"
28+
set +a
29+
30+
echo "[load-env] Loaded environment variables from $ENV_FILE"

0 commit comments

Comments
 (0)