You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Sequential Backslashes are removed during interpretation. #1106
Sequential Backslashes seem to be reduced to a single backslash. Is there a purposeful reason behind this or am I correct in believing this to be a bug?
Yup. Using the interp Go API for some shell scripts with gnarly arguments including some regexps. Some of the regexps were off after going through my program and after debugging, I managed to localize the issue to the library as above.
Further investigation seems to indicate this is a problem specific to double quoted literals. gosh -c "echo 'hello \\\\\\ world'" yields what I would expect: hello \\\ world.
Changing to the below seems to fix it and still pass all the test cases I could think of as relevant.
buf := cfg.strBuilder()
for i := 0; i < len(s); i++ {
b := s[i]
if b == '\\' && i+1 < len(s) {
switch s[i+1] {
case '"', '\\', '$', '`': // special chars
i++
b = s[i] // write the special char, skipping the backslash
}
}
buf.WriteByte(b)
}
Activity
mvdan commentedon Nov 1, 2024
Thanks for reporting, this does seem like a bug.
Out of curiosity, how did you find this? Are you using the interpreter via the Go API?
cfogrady commentedon Nov 1, 2024
Yup. Using the interp Go API for some shell scripts with gnarly arguments including some regexps. Some of the regexps were off after going through my program and after debugging, I managed to localize the issue to the library as above.
cfogrady commentedon Nov 1, 2024
Further investigation seems to indicate this is a problem specific to double quoted literals.
gosh -c "echo 'hello \\\\\\ world'"yields what I would expect:hello \\\ world.cfogrady commentedon Nov 1, 2024
I'll prep a PR after lunch.
Offending code seems to be: https://github.com/mvdan/sh/blob/392da98b03853d889822bc2f62d61f4f92f37fa4/expand/expand.go#L505C4-L518C5
Changing to the below seems to fix it and still pass all the test cases I could think of as relevant.
cfogrady commentedon Nov 4, 2024
Got side tracked by some other issues Friday afternoon. PR to fix the backslash issue: #1107
expand: fix sequential '\' in double quote literals
chore(deps): update module mvdan.cc/sh/v3/cmd/shfmt to v3.12.0 (hetzn…