Skip to content

Commit

Permalink
expand: "$@" should expand to no fields when $# == 0
Browse files Browse the repository at this point in the history
We had logic to always expand "..." to at least "", since that's how
double quotes in shell work. However, "$@" is an exception to that, so
it needs to be allowed to return zero fields.

Add a test case for the array version, too.

Fixes #473.
  • Loading branch information
mvdan committed Jan 11, 2020
1 parent c11932a commit feeb22f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@ func (cfg *Config) wordFields(wps []syntax.WordPart) ([][]fieldPart, error) {
}
curField = append(curField, fp)
case *syntax.DblQuoted:
allowEmpty = true
if len(x.Parts) == 1 {
pe, _ := x.Parts[0].(*syntax.ParamExp)
if elems := cfg.quotedElems(pe); elems != nil {
Expand All @@ -558,6 +557,7 @@ func (cfg *Config) wordFields(wps []syntax.WordPart) ([][]fieldPart, error) {
continue
}
}
allowEmpty = true
wfield, err := cfg.wordField(x.Parts, quoteDouble)
if err != nil {
return nil, 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 @@ -275,6 +275,10 @@ var runTests = []runTest{
{`echo -n "\\"`, `\`},
{`set -- a b c; x="$@"; echo "$x"`, "a b c\n"},
{`set -- b c; echo a"$@"d`, "ab cd\n"},
{`count() { echo $#; }; set --; count "$@"`, "0\n"},
{`count() { echo $#; }; set -- ""; count "$@"`, "1\n"},
{`count() { echo $#; }; a=(); count "${a[@]}"`, "0\n"},
{`count() { echo $#; }; a=(""); count "${a[@]}"`, "1\n"},
{`echo $1 $3; set -- a b c; echo $1 $3`, "\na c\n"},
{`[[ $0 == "bash" || $0 == "gosh" ]]`, ""},

Expand Down

0 comments on commit feeb22f

Please sign in to comment.