diff --git a/dotenv/fixtures/quoted.env b/dotenv/fixtures/quoted.env index 2f05d19f..a3afd517 100644 --- a/dotenv/fixtures/quoted.env +++ b/dotenv/fixtures/quoted.env @@ -11,4 +11,5 @@ OPTION_J = 'first line second line third line and so on' +OPTION_K='Let\'s go!' OPTION_Z = "last value" diff --git a/dotenv/godotenv_test.go b/dotenv/godotenv_test.go index 1b60f710..4f6588ec 100644 --- a/dotenv/godotenv_test.go +++ b/dotenv/godotenv_test.go @@ -171,6 +171,7 @@ func TestLoadQuotedEnv(t *testing.T) { second line third line and so on`, + "OPTION_K": "Let's go!", "OPTION_Z": "last value", } diff --git a/dotenv/parser.go b/dotenv/parser.go index 11b6d027..861cd953 100644 --- a/dotenv/parser.go +++ b/dotenv/parser.go @@ -159,27 +159,34 @@ func (p *parser) extractVarValue(src string, envMap map[string]string, lookupFn previousCharIsEscape := false // lookup quoted string terminator + var chars []byte for i := 1; i < len(src); i++ { - if src[i] == '\n' { + char := src[i] + if char == '\n' { p.line++ } - if char := src[i]; char != quote { + if char != quote { if !previousCharIsEscape && char == '\\' { previousCharIsEscape = true - } else { + continue + } + if previousCharIsEscape { previousCharIsEscape = false + chars = append(chars, '\\') } + chars = append(chars, char) continue } // skip escaped quote symbol (\" or \', depends on quote) if previousCharIsEscape { previousCharIsEscape = false + chars = append(chars, char) continue } // trim quotes - value := string(src[1:i]) + value := string(chars) if quote == prefixDoubleQuote { // expand standard shell escape sequences & then interpolate // variables on the result