While brace expansion like {1,2}
originates in csh
in the late 70s, and found its way to Bourne-like shells in bash
/zsh
/pdksh
in the late 80s, early 90s, the {n1..n2}
variant came later first in zsh
in 1995 (2.6-beta4).
bash
copied it in 2004 (3.0) and ksh93
in 2005 (ksh93r
).
Probably the shell you're trying this in is neither of those or is an older version of bash
and ksh93
.
What happens is that bash first expands *.djvu{,.bk}
into *.djvu
*.djvu.bk
, and then does glob-expansion on those. This would explain
what you observe: in your case, *.djvu
, matches an existing file,
say foo.djvu
and expands into that, but *.djvu.bk
matches no file,
and thus expands as itself, *.djvu.bk
.
The order of expansion is specified in the bash documentation:
The order of expansions is: brace expansion, tilde expansion, parame‐
ter, variable and arithmetic expansion and command substitution (done
in a left-to-right fashion), word splitting, and pathname expansion.
I would suggest rewriting your copy command as:
for f in *.djvu; do cp -- "$f" "$f".bk; done
Or perhaps, to avoid the syntactic overhead of an explicit for loop:
parallel -j1 cp -- {} {}.bk ::: *.djvu
(On second thoughts... that's not really much shorter.)
To answer your sub-question "how could it be expanded", one could use
a sub-command (example in a directory containing just foo.djvu
and
bar.djvu
):
$ echo $(echo *.djvu){,.bk}
bar.djvu foo.djvu bar.djvu foo.djvu.bk
But that isn't as safe a solution as the for loop or parallel call
above; it will break down on file names containing white space.
Best Answer
Yes, n > 1 is an explicit requirement:
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.