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.
The shell expands *
only if un-quoted, any quoting stops expansion by the shell.
Also, a brace expansion needs to be unquoted to be expanded by the shell.
This work (lets use echo to see what the shell does):
$ echo *.{ext1,ext2}
a.ext1 b.ext1 a.ext2 b.ext2
Even if there are files with some other names:
$ touch {a,b}.{ext1,ext2} {c,d}.{ext3,ext4} none
ls
a.ext1 a.ext2 b.ext1 b.ext2 c.ext3 c.ext4 d.ext3 d.ext4 none
$ echo *.{ext1,ext2}
a.ext1 b.ext1 a.ext2 b.ext2
Why that works?
It is important that we understand why that works. It is because of the order of expansion. First the "Brace expansion" and later (the last one) "Pathname Expansion" (a.k.a glob-expansion).
Brace --> Parameter (variable) --> Pathname
We can turn off "Pathname expansion" for a moment:
$ set -f
$ echo *.{ext1,ext2}
*.ext1 *.ext2
The "Pathname Expansion" receives two arguments: *.ext1
and *.ext2
.
$ set +f
$ echo *.{ext1,ext2}
a.ext1 b.ext1 a.ext2 b.ext2
The problem is that we can not use a variable for the brace expansion.
It has been explained many times before for using a variable inside a "Brace Expansion"
To expand a "Brace Expansion" that is the result of a "Variable Expansion", you need to re-submit the command line to the shell with eval
.
$ list={ext1,ext2}
$ eval echo '*.'"$list"
Brace --> Variable --> Glob || --> Brace --> Variable --> Glob
........ quoted here -->^^^^^^ || eval ^^^^^^^^^^^^^^^^^^^^^^^^^
Values of the file names bring no execution problem for eval:
$ touch 'a;date;.ext1'
eval echo '*.'"$list"
a;date;.ext1 a.ext1 b.ext1 a.ext2 b.ext2
But the value of $list
could be unsafe. However, the value of $list
is set by the script writer. The script writer is in control of eval
: Just not use externally set values for $list
. Try This:
#!/bin/bash
touch {a,b,c}.ext{1,2}
list=ext{1,2}
eval ls -l -- '*.'"$list"
A better alternative.
An alternative (without eval) is to use Bash "Extended Patterns":
#!/bin/bash
shopt -s extglob
list='@(ext1|ext2)'
ls -- *.$list
Note: Please be aware that both solutions (eval and patterns) (as written) are safe for filenames with spaces or new lines. But will fail for a $list
with spaces, because $list
is unquoted or the eval removes the quotes.
Best Answer
for i in {0..1000000}
andfor i in $(seq 1000000)
both build up a big list and then loop over it. That's inefficient and uses a lot of memory.Use:
instead. Or POSIXly:
Or:
To get a file full of CRLFs:
Generally, loops should be avoided when possible in shells.