Skip to content

Commit

Permalink
expand: fix sequential '\' in double quote literals
Browse files Browse the repository at this point in the history
Fixes #1106.
  • Loading branch information
cfogrady authored Nov 8, 2024
1 parent 392da98 commit 5919e5a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ func (cfg *Config) wordField(wps []syntax.WordPart, ql quoteLevel) ([]fieldPart,
if b == '\\' && i+1 < len(s) {
switch s[i+1] {
case '"', '\\', '$', '`': // special chars
continue
i++
b = s[i] // write the special char, skipping the backslash
}
}
sb.WriteByte(b)
Expand Down
5 changes: 5 additions & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ var runTests = []runTest{
{`echo \\\\`, "\\\\\n"},
{`echo \`, "\n"},

// escape characters in double quote literal
{`echo "\\"`, "\\\n"}, // special character is preserved
{`echo "\b"`, "\\b\n"}, // non-special character has both characters preserved
{`echo "\\\\"`, "\\\\\n"}, // sequential backslashes (escape characters repeated sequentially)

// vars
{"foo_interp_missing=bar_interp_missing; echo $foo_interp_missing", "bar_interp_missing\n"},
{"foo_interp_missing=bar_interp_missing foo_interp_missing=etc; echo $foo_interp_missing", "etc\n"},
Expand Down

0 comments on commit 5919e5a

Please sign in to comment.