Last Updated: February 25, 2016
·
954
· tylerhunt

Brace Expansion in Bash

In Bash, you can use curly braces to reduce the amount of typing needing when calling commands with multiple arguments.

$> mv path/to/{old_directory,new_directory}/file

I find this also comes in handy when creating a new file similar to others in the same directory:

$> vi path/to/{new_file.rb,*}

This will open the new file as the first buffer, and all the other files in the directory as additional buffers for reference.

You can use more than two strings in the expansion, too:

$> ls {.,lib,spec}/*.rb

Multiple expansions will result in every combination being expressed:

$> rm images/{v1,v2}/*.{gif,jpg,png}

If you want to see how a particular expansion will be handled, just replace the command with echo:

$> echo images/{v1,v2}/*.{gif,jpg,png}
images/v1/*.gif images/v1/*.jpg images/v1/*.png images/v2/*.gif images/v2/*.jpg images/v2/*.png

1 Response
Add your response

Excelent summary of brace expansion :)

I just want to add that you can use ranges of numbers and letters.

$ echo foo{1..5}
foo1 foo2 foo3 foo4 foo5
$ echo foo{a..c}
fooa foob fooc
over 1 year ago ·