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.
You can convert it to binary, reverse the bytes, optionally remove trailing newlines rev
<2.24, and convert it back:
$ xxd -revert -plain <<< 030201 | LC_ALL=C rev | tr -d '\n' | xxd -plain
010203
Using
$ bash --version | head -n1
GNU bash, version 4.3.42(1)-release (x86_64-redhat-linux-gnu)
$ xxd -version
xxd V1.10 27oct98 by Juergen Weigert
$ rev --version
rev from util-linux 2.28.2
This does not work if the string contains the NUL byte, because rev
will truncate the output at that point.
Best Answer
You can; you just need to break the range
{0..F}
into two separate ranges{0..9}
and{A..F}
: