mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
7.14k stars 338 forks source link

syntax: the printer can split up strings when joined with escaped newlines #832

Closed cilki closed 2 years ago

cilki commented 2 years ago

I came across this kind of assignment in a legacy codebase:

# Has the same effect as: wrapped='a b c'
wrapped=\
"a "\
"b "\
"c"

which shfmt turns into:

wrapped="a "\
    "b "\
    "c"

and causes the runtime error: line 4: b : command not found.

Tested on GNU bash, version 5.1.4(1)-release (x86_64-pc-linux-gnu) and the latest shfmt.

mvdan commented 2 years ago

Thanks, this does sound like a bug, probably in the printer. It should be taught to not indent after escaped newlines if that would change the program.