shell brace expansion (was "regex help")
Linux for blind general discussion
blinux-list at redhat.com
Wed Feb 9 22:43:24 UTC 2022
Tim again, replying inline
> whatever's in the braces is treated as a comma separated array of
> strings and whatever string the braces are part of gets duplicated
> for each entry in the array
Exactly.
> multiple sets of braces, a test I just did suggests the expansion
> covers the entire Cartesian product
Also correct.
> Is there a way to define a range? I tried:
As Didier pointed out, this functionality isn't defined in POSIX, so
it depends on which shell you're using. In most Linux distros, that's
bash which has both the above string brace-expansion, as well as
numeric-range expansion:
$ echo {1..5}
1 2 3 4 5
$ echo {1..10..2}
1 3 5 7 9
$ echo {5..1}
5 4 3 2 1
$ echo {10..1..-2}
10 8 6 4 2
whereas under the ksh shell on my OpenBSD box, it supports the string
brace-expansion, but not the numerical-range form.
So for some test cases, I've been known to use that Cartesian product
to do things like
$ mkdir -p {a,b,c}/{x,y,z}
$ echo hello | tee {a,b,c}/{x,y,z}/{1..5}
which produces 45 files in nested directories.
-Tim
More information about the Blinux-list
mailing list