@@ -159,6 +159,8 @@ func Document(cfg *Config, word *syntax.Word) (string, error) {
159159 return cfg .fieldJoin (field ), nil
160160}
161161
162+ const patMode = pattern .Filenames | pattern .Braces
163+
162164// Pattern expands a single shell word as a pattern, using syntax.QuotePattern
163165// on any non-quoted parts of the input word. The result can be used on
164166// syntax.TranslatePattern directly.
@@ -174,7 +176,7 @@ func Pattern(cfg *Config, word *syntax.Word) (string, error) {
174176 buf := cfg .strBuilder ()
175177 for _ , part := range field {
176178 if part .quote > quoteNone {
177- buf .WriteString (pattern .QuoteMeta (part .val ))
179+ buf .WriteString (pattern .QuoteMeta (part .val , patMode ))
178180 } else {
179181 buf .WriteString (part .val )
180182 }
@@ -345,11 +347,11 @@ func (cfg *Config) escapedGlobField(parts []fieldPart) (escaped string, glob boo
345347 buf := cfg .strBuilder ()
346348 for _ , part := range parts {
347349 if part .quote > quoteNone {
348- buf .WriteString (pattern .QuoteMeta (part .val ))
350+ buf .WriteString (pattern .QuoteMeta (part .val , patMode ))
349351 continue
350352 }
351353 buf .WriteString (part .val )
352- if pattern .HasMeta (part .val ) {
354+ if pattern .HasMeta (part .val , patMode ) {
353355 glob = true
354356 }
355357 }
@@ -367,9 +369,10 @@ func Fields(cfg *Config, words ...*syntax.Word) ([]string, error) {
367369 fields := make ([]string , 0 , len (words ))
368370 dir := cfg .envGet ("PWD" )
369371 for _ , word := range words {
370- afterBraces := []* syntax.Word {word }
371- if syntax .SplitBraces (word ) {
372- afterBraces = Braces (word )
372+ word := * word // make a copy, since SplitBraces replaces the Parts slice
373+ afterBraces := []* syntax.Word {& word }
374+ if syntax .SplitBraces (& word ) {
375+ afterBraces = Braces (& word )
373376 }
374377 for _ , word2 := range afterBraces {
375378 wfields , err := cfg .wordFields (word2 .Parts )
@@ -540,7 +543,6 @@ func (cfg *Config) wordFields(wps []syntax.WordPart) ([][]fieldPart, error) {
540543 }
541544 curField = append (curField , fp )
542545 case * syntax.DblQuoted :
543- allowEmpty = true
544546 if len (x .Parts ) == 1 {
545547 pe , _ := x .Parts [0 ].(* syntax.ParamExp )
546548 if elems := cfg .quotedElems (pe ); elems != nil {
@@ -556,6 +558,7 @@ func (cfg *Config) wordFields(wps []syntax.WordPart) ([][]fieldPart, error) {
556558 continue
557559 }
558560 }
561+ allowEmpty = true
559562 wfield , err := cfg .wordField (x .Parts , quoteDouble )
560563 if err != nil {
561564 return nil , err
0 commit comments