CROSS JOIN via brace expansion
I needed to create some files in the shell and I used a brace expansion to do it:
mkdir -p static/{css,js,images}
This code creates this folder structure.
static
├── css
├── images
└── js
This is quite cool! But it also turns out that if you put a brace expansion next to another brace expansion, bash will give you every permutation of the contents of all the braces - a cartesian product, like a CROSS JOIN in a database.
This means, for instance, you can compute all the cards in Set from the shell like this:
echo {1-,2-,3-}{empty-,lined-,solid-}{red-,green-,purple-}{diamond,squiggle,lozenge} | tr " " "\n"
which yields
1-empty-red-diamond
1-empty-red-squiggle
1-empty-red-lozenge
1-empty-green-diamond
1-empty-green-squiggle
1-empty-green-lozenge
1-empty-purple-diamond
1-empty-purple-squiggle
1-empty-purple-lozenge
1-lined-red-diamond
... etc
You can verify it’s correct by piping the output to wc -w
to count the
words. The answer is 81: the number of cards in the Set deck.