What if you could create ten folders with just one simple command instead of ten?
Why Brace expansion ({1..10}) in Bash Scripting? - Purpose & Use Cases
Imagine you need to create 10 folders named folder1 to folder10 on your computer. Doing this by typing each command one by one feels like writing the same sentence over and over again.
Typing commands manually is slow and boring. It's easy to make mistakes like skipping a number or typing the wrong name. If you want to change the range, you have to rewrite many lines.
Brace expansion lets you write a simple pattern that automatically creates all the names you need. Instead of repeating commands, you write one line that expands into many, saving time and avoiding errors.
mkdir folder1 mkdir folder2 mkdir folder3 ...mkdir folder10
mkdir folder{1..10}It makes repetitive tasks fast and error-free by generating many items from a simple pattern.
When organizing photos by date, you can quickly create folders for each day of the month without typing each folder name.
Manual repetition is slow and error-prone.
Brace expansion creates many items from one pattern.
This saves time and reduces mistakes in scripts.
Practice
{1..5} produce when used in a command?Solution
Step 1: Understand brace expansion syntax
The syntax{start..end}generates a sequence of numbers or letters from start to end.Step 2: Apply to numbers 1 to 5
Using{1..5}produces the numbers 1, 2, 3, 4, 5 separated by spaces.Final Answer:
A sequence of numbers: 1 2 3 4 5 -> Option AQuick Check:
Brace expansion {1..5} = 1 2 3 4 5 [OK]
- Thinking it outputs letters for numbers
- Expecting a syntax error
- Assuming it outputs the literal string
Solution
Step 1: Recall correct brace expansion syntax
Brace expansion uses curly braces with two dots:{start..end}.Step 2: Check each option
Onlyecho {3..7}uses correct braces and dots. Others use wrong symbols like parentheses, brackets, or hyphens.Final Answer:
echo {3..7} -> Option DQuick Check:
Correct syntax uses curly braces and two dots [OK]
- Using parentheses or brackets instead of braces
- Using a hyphen instead of two dots
- Missing braces entirely
echo file{1..3}.txtSolution
Step 1: Understand brace expansion with suffix
The brace expansion{1..3}generates 1 2 3, and the suffix.txtis added to each.Step 2: Combine parts
Each expanded number is combined with 'file' prefix and '.txt' suffix, producing: file1.txt file2.txt file3.txt.Final Answer:
file1.txt file2.txt file3.txt -> Option AQuick Check:
Brace expansion with suffix repeats suffix for each number [OK]
- Expecting literal braces in output
- Missing suffix on some items
- Mixing suffix placement
echo {1..5..2}Solution
Step 1: Understand brace expansion with step
The syntax{start..end..step}is valid in Bash to generate sequences with a step.Step 2: Apply to {1..5..2}
This generates numbers starting at 1, stepping by 2, up to 5: 1, 3, 5.Final Answer:
No error; outputs 1 3 5 -> Option CQuick Check:
Brace expansion supports step with two dots between numbers [OK]
- Thinking extra dots cause syntax error
- Expecting continuous sequence ignoring step
- Assuming no output for step usage
log1.txt through log10.txt using a single Bash command with brace expansion. Which command achieves this?Solution
Step 1: Understand the goal
You want files named log1.txt, log2.txt, ..., log10.txt created.Step 2: Use brace expansion with suffix
The correct syntax istouch log{1..10}.txt, which expands to log1.txt, log2.txt, ..., log10.txt.Step 3: Check other options
touch log{1..10}omits the .txt suffix;touch log{1..10.txt}has invalid syntax with .txt inside braces;touch log{1..10}.txt.txtadds an extra .txt to each filename.Final Answer:
touch log{1..10}.txt -> Option BQuick Check:
Brace expansion with suffix creates all files at once [OK]
- Placing suffix inside braces incorrectly
- Adding extra suffixes
- Omitting suffix leading to wrong filenames
Ready to prove it?
Pass the test and this pattern moves unknown
