direnv/direnv · error

unclosed quoted value in .env file

Error message

unclosed quoted value in .env file

What it means

The dotenv parser tracks multi-line quoted values; if input ends while a quoted value is still open (no closing quote on any subsequent line), Parse returns this error. It protects against silently truncating a value that was meant to span multiple lines.

Source

Thrown at pkg/dotenv/parse.go:116

		match := lineRe.FindStringSubmatch(line)
		// commented or empty line
		if len(match) == 0 {
			continue
		}
		if len(match[1]) == 0 {
			continue
		}

		key := match[1]
		value := match[2]

		parseValue(key, value, dotenv)
	}

	// If we end with an unclosed multi-line value, return an error
	if inMultiline {
		return nil, fmt.Errorf("unclosed quoted value in .env file")
	}

	return dotenv, nil
}

// MustParse works the same as Parse but panics on error
func MustParse(data string) map[string]string {
	env, err := Parse(data)
	if err != nil {
		panic(err)
	}
	return env
}

func parseValue(key string, value string, dotenv map[string]string) {
	if len(value) <= 1 {
		dotenv[key] = value
		return

View on GitHub (pinned to b00e451f54)

Solutions

  1. Add the missing closing quote to the value in the .env file
  2. Check the last value in the file — the unclosed one starts there
  3. For multi-line content prefer single-quoted multi-line values and ensure EOF terminates after the quote
  4. Regenerate the .env from a template to restore lost characters

Example fix

// before
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
MIIE...
// after
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
MIIE...
-----END RSA PRIVATE KEY-----"
Defensive patterns

Strategy: validation

Validate before calling

awk "'{n+=gsub(/\"/,"\"")} END{if (n%2) exit 1}' .env || echo 'unbalanced quotes'

Try / catch

env, err := dotenv.Parse(data)
if err != nil { return fmt.Errorf("dotenv parse: %w", err) } // catches unclosed quote too

Prevention

When it happens

Trigger: Parse() finishes reading the file while inMultiline is true — a value opened with `KEY="...` or `KEY='...` never received its closing quote before EOF.

Common situations: A quote deleted accidentally during editing; copying a multi-line secret (PEM key, JSON) into .env and missing the final quote; text editors truncating the last line; shell heredoc mistakes when generating .env.

Related errors


AI-assisted analysis of direnv/direnv@b00e451f54 (2026-09-05). Data as JSON: /api/errors/06ff6e57a6cd2de9. Report an issue: GitHub.