Examples of brace expansion in linux/unix

Brace expansion in linux/unix can help us generate multiple strings in batches, which are mainly used in the command line or shell script.


Bracket expansion consists of a sequence description or a comma-separated list of items within the braces “{}”.


A sequence consists of a start and end item, separated by two periods “..”.

Syntax
{item1,item2,item3 ...}

{M..N}

Examples
{str1,str2,str3,str3}  => str1 str2 str3 str4
{0..5}                 => 0 1 2 3 4 5 
{2..-2}                => 2 1 0 -1 -2
{a..e}                 => a b c d e
{e..a}                 => e d c b a

Use prefix or suffix string bracket expansion

{1..3}.log      =>  1.log 2.log 3.log

Use nested bracket expansion

{a,{0..2}b,c}   => a 0b 1b 2b c

Bracket expansion on the command line

prints out the numbers from 0 to 10. Using:

➜  ~ echo {0..10}
0 1 2 3 4 5 6 7 8 9 10

Print nested bracket expansion. Using:

➜  ~ echo {a,{0..2}b,c}
a 0b 1b 2b c

Create 1 to 10.log sequence file. Using:

➜  ~ touch {1..10}.log

Prints every second number, starting with 10 and making its way backwards to 0. Using:

➜  ~ echo {10..0..2}
10 8 6 4 2 0

Delete multiple files in batch. Using:

➜  ~ rm /path/{foo,bar}

Add a Comment

Your email address will not be published.