Bash – Brace expansion does not work for less than 2 arguments

bashbrace-expansion

I was expecting brace expansion to work for any number of arguments. However, for n=1 I get the following:

$ find models/nsf-projects-{7}*
models/nsf-projects-{7}.rdf

For n>1 expansion occurs as expected, e.g.:

$ find models/nsf-projects-{6,7}*
find: ‘models/nsf-projects-6*’: No such file or directory
find: ‘models/nsf-projects-7*’: No such file or directory

I have browsed the GNU manuals a bit, but have not found the requirement to >1 arguments stated explicitly anywhere.

Q: Is n>1 indeed a requirement for brace expansion? If so, why is it useful?

Best Answer

Yes, n > 1 is an explicit requirement:

A correctly-formed brace expansion must contain unquoted opening and closing braces, and at least one unquoted comma or a valid sequence expression. Any incorrectly formed brace expansion is left unchanged.

As for the why - historical reasons, to some extent (though it was copied from csh originally, which has the other behaviour). There are commands that take {} as a literal argument (find, parallel, and others with more complex arguments), and also other uses of {} in the shell language. Because brace expansions are only processed when written literally (and not from variables), there's really no motivation to support degenerate expansions, and some reasons not to.

Related Question