Monday, March 31, 2008

Old Dogs, New Tricks: Brace Expansion

I revisit my habits a lot.

It's a sentinel. You can't teach an old dog new tricks, so when I start being able to learn new things, I'll know I'm entering my second childhood.

For years, I used square brackets to match filenames. Now, I use the newer, Berkeley-esque, curlies more often.
$ echo x.[ab]
x.a x.b
$ echo x.{a,b}
x.a x.b
They're different. For curlies, order counts:
$ echo x.[ba]
x.a x.b
$ echo x.{b,a}
x.b x.a
Also, x.[ba] must match something.
$ mkdir /tmp/FOO; cd /tmp/FOO
$ ls # nothing there
$ touch x.[ba]
$ ls
x.[ba] # one file!
$ rm -f *
$ touch x.{b,a}
$ ls
x.a x.b
They're conceptually different. While square brackets are a file-name-match operation, curlies are text-substitution. When you use square brackets, you're asking for existing files that match the pattern; when you use curlies, it's a typing shorthand that the shell expands for you before it does anything else.

The items can nest, and they don't even have to have anything in them.
$ echo x{,a,b{s,t}}
x xa xbs xbt
Curly-expansion is officially called brace expansion, and square-bracket expansion is part of pathname expansion, or (more commonly) globbing.

No comments: