Skip to content

Instantly share code, notes, and snippets.

@lacostenycoder
Created March 13, 2025 20:02
Show Gist options
  • Save lacostenycoder/e42f0d3dcc7589e93a1711f98e165ebb to your computer and use it in GitHub Desktop.
Save lacostenycoder/e42f0d3dcc7589e93a1711f98e165ebb to your computer and use it in GitHub Desktop.
Find used environment variables using git grep -i
#!/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