Skip to content

fix: replace awk-based .env parsing with read loop to handle values containing spaces#83

Open
amathxbt wants to merge 1 commit intoinkonchain:mainfrom
amathxbt:fix/dotenv-parsing-robust
Open

fix: replace awk-based .env parsing with read loop to handle values containing spaces#83
amathxbt wants to merge 1 commit intoinkonchain:mainfrom
amathxbt:fix/dotenv-parsing-robust

Conversation

@amathxbt
Copy link
Copy Markdown

Bug

progress.sh loads .env using awk field splitting:

export $(cat .env | grep -v '#' | sed 's/\r$//' | awk '/=/ {print $1}' ) || error_exit "Failed to load .env file"

awk splits on whitespace by default. This means any .env value that contains a space is silently truncated:

# .env
ETH_RPC_URL=http://localhost:8545  # works fine
SOME_KEY=value with spaces         # ❌ awk prints only "SOME_KEY=value"

Additionally:

  • grep -v '#' strips entire lines containing # anywhere, not just comment lines — it would incorrectly strip SOME_KEY=value#tag
  • The || error_exit guard never fires on parsing errors because export of an empty string list succeeds silently
  • export $(...) with word-split output is fragile: if a value accidentally contains shell metacharacters, it can cause unexpected behaviour

Fix

Replace the one-liner with a while read loop that processes the file line-by-line, correctly skipping blank lines and comment lines while exporting each assignment verbatim:

if [ -f .env ]; then
    while IFS= read -r line || [ -n "$line" ]; do
        # Skip blank lines and comment lines
        [[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
        # Only export lines that contain an assignment
        if [[ "$line" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
            export "$line"
        fi
    done < .env
fi

This correctly handles:

  • Values with spaces (KEY=value with spaces)
  • Inline comments being preserved as part of values (consistent with dotenv spec)
  • Files that do not end with a newline (the || [ -n "$line" ] condition)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant