Bash Brace Expansion In Linux with Examples
Brасe exраnsiоn is а wаy by whiсh аrbitrаry strings саn be generаted frоm the terminаl оr by using any bаsh sсriрt, it allows you to create multiple modified command-line arguments out of a single argument. The syntаx fоr brасe exраnsiоn соnsists оf either a sequenсe sрeсifiсаtiоn оr а cоmmа seраrаted list оf dаtа inside сurly brасes “{}”. The раttern shоuld nоt cоntаin embedded whitesрасe. There are two optional parts to Brace expansion — Preamble and Postscript. The Preamble is prefixed to each string contained within the braces, and the Postscript is then appended to each resulting string, expanding left to right.
Method 1: Using Comma-separated lists
echo {Beginner,for,Beginner} echo {"hello","world"}
Method 2: Using Ranges, Different types of ranges can be used, like numeric, alphabetic, or both. A sequence consists of a starting and ending term separated by 2 periods “..” inside curly braces “{}”.
echo {A..D} echo {3..8} echo {A..C}{1..2}
Method 3: Using Preamble, Patterns to be brace expanded may contain a leading term called a preamble. The brace expression may contain either a comma-separated list or a range.
echo gfg{1..4} echo gfg{1,5,9}
Method 4: Using Postscript, Like Preamble, it can also have an ending portion known as Postscript.
echo {1..3}gfg echo {1,5,9}gfg
Bash Expansion can be nested.
echo {a,b{1..3},c}
Bash Expansion can be used with other commands also, like, we can make multiple folders with one command using Brace expansion with mkdir command.