bash-guide icon indicating copy to clipboard operation
bash-guide copied to clipboard

Add for using brace or nested brace expansion

Open khanhnt2 opened this issue 7 years ago • 1 comments

There is an other use of for, it's very useful in some case

for char in {a..z}; do
    echo$ char
done

It will be print a b c ... x y z or

for i in {{a..m},{0..9},{n..z}}; do # don't have any space between commas
    echo $i
done

It will be print a b c ... k l m 0 1 2 ... 7 8 9 n o u ... x y z

khanhnt2 avatar May 13 '17 13:05 khanhnt2

For anyone like me seeing this while searching through GitHub, this is not a feature of the for loop itself, but a general feature called brace expansion. This is useful in a great many ways, such as during file operations with glob filename pattern matching and in the situation posted here.

terminalforlife avatar May 14 '23 23:05 terminalforlife