diff --git a/fixtures/quoted.env b/fixtures/quoted.env index ce77cfb..1092b76 100644 --- a/fixtures/quoted.env +++ b/fixtures/quoted.env @@ -8,3 +8,4 @@ OPTION_G="" OPTION_H="\n" OPTION_I="some multi-line text with \"escaped quotes\" and ${OPTION_A} variable" +OPTION_J='some$pecial$1$2!*chars=qweq""e$$\$""' diff --git a/gotenv.go b/gotenv.go index 0d0e2d5..c4c1e50 100644 --- a/gotenv.go +++ b/gotenv.go @@ -198,17 +198,19 @@ func parseLine(s string, env Env, override bool) error { key := rm[1] val := rm[2] + // trim whitespace + val = strings.TrimSpace(val) + // determine if string has quote prefix hdq := strings.HasPrefix(val, `"`) // determine if string has single quote prefix hsq := strings.HasPrefix(val, `'`) - // trim whitespace - val = strings.Trim(val, " ") - // remove quotes '' or "" - val = strings.Trim(val, `'"`) + if l := len(val); (hsq || hdq) && l >= 2 { + val = val[1 : l-1] + } if hdq { val = strings.ReplaceAll(val, `\n`, "\n") @@ -222,8 +224,10 @@ func parseLine(s string, env Env, override bool) error { return varReplacement(s, hsq, env, override) } - val = varRgx.ReplaceAllStringFunc(val, fv) - val = parseVal(val, env, hdq, override) + if !hsq { + val = varRgx.ReplaceAllStringFunc(val, fv) + val = parseVal(val, env, hdq, override) + } env[key] = val return nil diff --git a/gotenv_test.go b/gotenv_test.go index 21c7148..4c08df1 100644 --- a/gotenv_test.go +++ b/gotenv_test.go @@ -199,6 +199,7 @@ THE SOFTWARE.`, "OPTION_H": "\n", "OPTION_I": `some multi-line text with "escaped quotes" and 1 variable`, + "OPTION_J": `some$pecial$1$2!*chars=qweq""e$$\$""`, }, }, {