Bird
Raised Fist0
Bash Scriptingscripting~5 mins

Brace expansion ({1..10}) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the brace expansion {1..10} do in bash?
It generates a sequence of numbers from 1 to 10, expanding to 1 2 3 4 5 6 7 8 9 10.
Click to reveal answer
beginner
How can you use brace expansion to create files named file1.txt to file10.txt?
Use the command touch file{1..10}.txt. This creates 10 files named file1.txt, file2.txt, ..., file10.txt.
Click to reveal answer
intermediate
Can brace expansion generate sequences with steps, like every 2 numbers from 1 to 10?
Yes, by using {start..end..step}. For example, {1..10..2} expands to 1 3 5 7 9.
Click to reveal answer
beginner
What happens if you use brace expansion with letters, like {a..e}?
It generates a sequence of letters from a to e: a b c d e.
Click to reveal answer
intermediate
Is brace expansion performed before or after variable expansion in bash?
Brace expansion happens before variable expansion. So variables inside braces won't expand as part of the brace expansion.
Click to reveal answer
What is the output of echo {3..7} in bash?
A3..7
B{3..7}
C3 4 5 6 7
D7 6 5 4 3
Which command creates files named test1.txt to test5.txt using brace expansion?
Atouch test[1-5].txt
Btouch test{1..5}.txt
Ctouch test{1,5}.txt
Dtouch test1.txt test5.txt
How do you generate odd numbers from 1 to 9 using brace expansion?
A{1..9..2}
B{1..9..3}
C{1..9}
D{2..10..2}
What does echo {z..v} output?
Az y x w v
Bv w x y z
C{z..v}
DError
If you run echo file{$i..10}.txt with i=3, what happens?
AExpands to file3.txt file4.txt ... file10.txt
BOutputs file3..10.txt
CError because variable inside braces
DOutputs file{$i..10}.txt literally
Explain how brace expansion works in bash using the example {1..10}.
Think about how you can quickly list numbers without typing each one.
You got /3 concepts.
    Describe how to create multiple files named file1.txt to file10.txt using a single bash command.
    Use brace expansion inside the file name.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does the Bash brace expansion {1..5} produce when used in a command?
      easy
      A. A sequence of numbers: 1 2 3 4 5
      B. A sequence of letters: a b c d e
      C. A syntax error
      D. A single string: {1..5}

      Solution

      1. Step 1: Understand brace expansion syntax

        The syntax {start..end} generates a sequence of numbers or letters from start to end.
      2. Step 2: Apply to numbers 1 to 5

        Using {1..5} produces the numbers 1, 2, 3, 4, 5 separated by spaces.
      3. Final Answer:

        A sequence of numbers: 1 2 3 4 5 -> Option A
      4. Quick Check:

        Brace expansion {1..5} = 1 2 3 4 5 [OK]
      Hint: Brace expansion {x..y} lists numbers or letters from x to y [OK]
      Common Mistakes:
      • Thinking it outputs letters for numbers
      • Expecting a syntax error
      • Assuming it outputs the literal string
      2. Which of the following is the correct syntax to generate numbers from 3 to 7 using brace expansion in Bash?
      easy
      A. echo [3..7]
      B. echo {3-7}
      C. echo (3..7)
      D. echo {3..7}

      Solution

      1. Step 1: Recall correct brace expansion syntax

        Brace expansion uses curly braces with two dots: {start..end}.
      2. Step 2: Check each option

        Only echo {3..7} uses correct braces and dots. Others use wrong symbols like parentheses, brackets, or hyphens.
      3. Final Answer:

        echo {3..7} -> Option D
      4. Quick Check:

        Correct syntax uses curly braces and two dots [OK]
      Hint: Use curly braces and two dots for sequences {start..end} [OK]
      Common Mistakes:
      • Using parentheses or brackets instead of braces
      • Using a hyphen instead of two dots
      • Missing braces entirely
      3. What is the output of the following Bash command?
      echo file{1..3}.txt
      medium
      A. file1.txt file2.txt file3.txt
      B. file{1..3}.txt
      C. file1 file2 file3.txt
      D. file1.txt file2.txt file3

      Solution

      1. Step 1: Understand brace expansion with suffix

        The brace expansion {1..3} generates 1 2 3, and the suffix .txt is added to each.
      2. Step 2: Combine parts

        Each expanded number is combined with 'file' prefix and '.txt' suffix, producing: file1.txt file2.txt file3.txt.
      3. Final Answer:

        file1.txt file2.txt file3.txt -> Option A
      4. Quick Check:

        Brace expansion with suffix repeats suffix for each number [OK]
      Hint: Brace expansion repeats suffix/prefix for each number [OK]
      Common Mistakes:
      • Expecting literal braces in output
      • Missing suffix on some items
      • Mixing suffix placement
      4. Identify the error in this Bash command:
      echo {1..5..2}
      medium
      A. Outputs 1 2 3 4 5
      B. Syntax error: too many dots
      C. No error; outputs 1 3 5
      D. Outputs nothing

      Solution

      1. Step 1: Understand brace expansion with step

        The syntax {start..end..step} is valid in Bash to generate sequences with a step.
      2. Step 2: Apply to {1..5..2}

        This generates numbers starting at 1, stepping by 2, up to 5: 1, 3, 5.
      3. Final Answer:

        No error; outputs 1 3 5 -> Option C
      4. Quick Check:

        Brace expansion supports step with two dots between numbers [OK]
      Hint: Brace expansion can include step: {start..end..step} [OK]
      Common Mistakes:
      • Thinking extra dots cause syntax error
      • Expecting continuous sequence ignoring step
      • Assuming no output for step usage
      5. You want to create empty files named log1.txt through log10.txt using a single Bash command with brace expansion. Which command achieves this?
      hard
      A. touch log{1..10}
      B. touch log{1..10}.txt
      C. touch log{1..10}.txt.txt
      D. touch log{1..10.txt}

      Solution

      1. Step 1: Understand the goal

        You want files named log1.txt, log2.txt, ..., log10.txt created.
      2. Step 2: Use brace expansion with suffix

        The correct syntax is touch log{1..10}.txt, which expands to log1.txt, log2.txt, ..., log10.txt.
      3. 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.txt adds an extra .txt to each filename.
      4. Final Answer:

        touch log{1..10}.txt -> Option B
      5. Quick Check:

        Brace expansion with suffix creates all files at once [OK]
      Hint: Use touch with brace expansion and suffix: touch name{1..n}.ext [OK]
      Common Mistakes:
      • Placing suffix inside braces incorrectly
      • Adding extra suffixes
      • Omitting suffix leading to wrong filenames
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard