4

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?

8

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.

5

man bash states (emphasis added):

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.

That is also the fifth paragraph in the Brace Expansion section of the bash manual.

I can only speculate as to the motivation, but I suspect it was to allow you to type things which contain {...} without the braces vanishing. (That would apply to regular expression repetition operators, to take just one example.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.