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.
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.
For example, make a backup of all your files in a directory:
for i in * ; do
cp "$i"{,.bak}
done
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.
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
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.