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 into
inkonchain: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 into
inkonchain: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)

@kutluhaneth46 kutluhaneth46 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

progress.sh was doing export $(cat .env | awk '/=/ {print $1}'), which only exports the key token before the first whitespace and breaks on values containing spaces (and is generally fragile). The while read + KEY=VALUE regex + export "$line" approach is the right fix for this script.

Minor non-blocking notes:

  • Values with embedded shell metacharacters still rely on .env being trusted local config (same as before).
  • Consider sed 's/\r$//' if anyone ever drops a CRLF .env on Windows.

LGTM for the stated bug.

@kutluhaneth46 kutluhaneth46 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

progress.sh was doing export $(cat .env | awk '/=/ {print $1}'), which only exports the key token before the first whitespace and breaks on values containing spaces (and is generally fragile). The while read + KEY=VALUE regex + export "$line" approach is the right fix for this script.

Minor non-blocking notes:

  • Values with embedded shell metacharacters still rely on .env being trusted local config (same as before).
  • Consider sed 's/\r$//' if anyone ever drops a CRLF .env on Windows.

Looks correct for the stated bug — thanks.

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.

2 participants