Skip to content
This repository was archived by the owner on Jan 12, 2022. It is now read-only.

Commit 5d289f4

Browse files
committed
escape some other bash-y special chars ($!)
1 parent 88e7c8b commit 5d289f4

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

godotenv.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"strings"
2525
)
2626

27+
const doubleQuoteSpecialChars = "\\\n\r\"!$`"
28+
2729
// Load will read your env file(s) and load them into ENV for this process.
2830
//
2931
// Call this function as close as possible to the start of your program (ideally in main)
@@ -297,9 +299,15 @@ func isIgnoredLine(line string) bool {
297299
}
298300

299301
func doubleQuoteEscape(line string) string {
300-
line = strings.Replace(line, `\`, `\\`, -1)
301-
line = strings.Replace(line, "\n", `\n`, -1)
302-
line = strings.Replace(line, "\r", `\r`, -1)
303-
line = strings.Replace(line, `"`, `\"`, -1)
302+
for _, c := range doubleQuoteSpecialChars {
303+
toReplace := "\\" + string(c)
304+
if c == '\n' {
305+
toReplace = `\n`
306+
}
307+
if c == '\r' {
308+
toReplace = `\r`
309+
}
310+
line = strings.Replace(line, string(c), toReplace, -1)
311+
}
304312
return line
305313
}

godotenv_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ func TestWrite(t *testing.T) {
346346
writeAndCompare(`key=va"lu"e`, `key="va\"lu\"e"`)
347347
//but single quotes are left alone
348348
writeAndCompare(`key=va'lu'e`, `key="va'lu'e"`)
349-
// newlines and backslashes are escaped
350-
writeAndCompare(`foo="ba\n\r\\r!"`, `foo="ba\n\r\\r!"`)
349+
// newlines, backslashes, and some other special chars are escaped
350+
writeAndCompare(`foo="$ba\n\r\\r!"`, `foo="\$ba\n\r\\r\!"`)
351351
}
352352

353353
func TestRoundtrip(t *testing.T) {

0 commit comments

Comments
 (0)