Skip to content

Commit

Permalink
expand: don't unescape characters inside here-documents
Browse files Browse the repository at this point in the history
I mistakenly understood that here-documents behaved more like
double quotes in this way, but they do not.

Fixes #1070.
  • Loading branch information
mvdan committed Feb 9, 2025
1 parent 59da037 commit 5a9a6f1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ func Literal(cfg *Config, word *syntax.Word) (string, error) {
return cfg.fieldJoin(field), nil
}

// Document expands a single shell word as if it were within double quotes. It
// is similar to Literal, but without brace expansion, tilde expansion, and
// Document expands a single shell word as if it were a here-document body.
// It is similar to [Literal], but without brace expansion, tilde expansion, and
// globbing.
//
// The config specifies shell expansion options; nil behaves the same as an
Expand All @@ -193,7 +193,7 @@ func Document(cfg *Config, word *syntax.Word) (string, error) {
return "", nil
}
cfg = prepareConfig(cfg)
field, err := cfg.wordField(word.Parts, quoteDouble)
field, err := cfg.wordField(word.Parts, quoteSingle)
if err != nil {
return "", err
}
Expand Down
4 changes: 4 additions & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,10 @@ var runTests = []runTest{
"cat <<'EOF'\nfoo_interp_missing\\\nbar_interp_missing\nEOF",
"foo_interp_missing\\\nbar_interp_missing\n",
},
{
"cat <<EOF\nfoo\\\"bar\\baz\nEOF",
"foo\\\"bar\\baz\n",
},
{
"mkdir a; echo foo_interp_missing >a |& grep -q 'is a directory'",
" #IGNORE bash prints a warning",
Expand Down

0 comments on commit 5a9a6f1

Please sign in to comment.