Benjamin
Benjamin

Reputation: 10403

When do you use brace expansion?

I understood what brace expansion is.
But I don't know where I use that.

When do you use it?
Please give me some convenient examples.

Thanks.

Upvotes: 6

Views: 331

Answers (4)

ghostdog74
ghostdog74

Reputation: 342949

In bash, you use brace expansion if you want to create a range, eg

for r in {0..100}

for r in {0..10..2} #with step of 2

for z in {a..z}

Instead of using external commands such as seq 0 100. Also , brace expansion can be used to list file types, eg

for file in *.{txt,jpg}.

This list all files that has txt and jpg extensions.

Upvotes: 2

Dennis Williamson
Dennis Williamson

Reputation: 360605

The range expression form of brace expansion is used in place of seq in a for loop:

for i in {1..100}
do
    something    # 100 times
done

Upvotes: 4

Marcelo Cantos
Marcelo Cantos

Reputation: 186068

You use it whenever you want to match against multiple choices. E.g.,

ls src/{Debug,Release}/*.o  # List all .o files in the Debug and Release directories.

Upvotes: 0

Benoit
Benoit

Reputation: 79233

For example, make a backup of all your files in a directory:

for i in * ; do
    cp "$i"{,.bak}
done

Upvotes: 2

Related Questions