Skip to main content
Welcome guest. | Register | Login | Post

Curly Braces are Damn Useful Things

In the bash shell curly braces { and } are surprisingly useful things to reduce typing.

With the mkdir command they help you create trees of directories in one command. For instance:

mkdir -p foo/{bar,baz,goo/loo/imp}/build will create the following directory structure:

foo/bar/build
foo/baz/build
foo/goo/loo/imp/build

The curly braces can also be used with other commands such as cp and mv. E.g. you want to move file foo/bar/a.out to foo/bar/hello you can use the following command:

mv foo/bar/{a.out,hello}

The source file (a.out) is separated from the destination file (hello) within the curly braces by the comma (,).

The curly braces are used in much the same way with cp. For example if you want to copy foo/bar/hello to foo/baz/goodbye you would use the following command:

cp foo/{bar/hello,baz/goodbye}

Comments

Very nice. Thanks for

Very nice. Thanks for sharing.

I don't know whether this

I don't know whether this is standard bourne, but it also works on zsh. Last time I checked, bash had lousy {} tab completition though (contrary to zsh)

It still has lousy {} tab

It still has lousy {} tab compelatiton free-zombie. I think I need to try out zsh, as I have been hearing quite a few interesting things about it of late.

I also haven't been playing

 

I also haven't been playing with zsh yet, mostly bash, and have tcsh installed since something wanted to have it, but I don't use it either.

But these braces really are a blessing, makes working with multiple files/directories really easy.
As far as I remember you can even have braces inside braces, like this:
mkdir ~/{{,s}bin,lib}
Which then will create:
~/bin
~/sbin
~/lib

Also stuff like this is possible:

mkdir ~/{bin,lib}{,64}
Which in turn creates:
~/bin
~/bin64
~/lib
~/lib64

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.