Created
March 13, 2025 20:02
-
-
Save lacostenycoder/e42f0d3dcc7589e93a1711f98e165ebb to your computer and use it in GitHub Desktop.
Find used environment variables using git grep -i
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# check_env_usage.sh | |
# Loops over .env line by line, extracts each ENV_VAR key, | |
# then uses 'git grep -i' to see if that key is referenced in the code. | |
# If it is found, log the key to 'required_vars.log'. | |
ENV_FILE=".env" | |
LOG_FILE="required_vars.log" | |
# Start fresh | |
> "$LOG_FILE" | |
while IFS= read -r line; do | |
# Skip empty lines or lines without '=' | |
[[ -z "$line" || "$line" != *=* ]] && continue | |
# Extract the key (left of the '='), stripping whitespace | |
KEY=$(echo "$line" | cut -d= -f1 | xargs) | |
# If KEY is empty (e.g. malformed line), skip | |
[[ -z "$KEY" ]] && continue | |
# Use git grep -i for a case-insensitive search | |
if git grep -iq "$KEY"; then | |
echo "$KEY" >> "$LOG_FILE" | |
fi | |
done < "$ENV_FILE" | |
echo "Done. 'required_vars.log' now contains any .env keys found in code." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment