Recall & Review
beginner
What does the syntax
for i in {1..10} do in a bash script?It creates a loop that runs 10 times, with the variable
i taking values from 1 to 10 in order.Click to reveal answer
beginner
How do you print numbers 1 to 10 using a for loop with range in bash?
Use:<br>
for i in {1..10}; do<br> echo $i<br>done<br>This prints numbers 1 through 10, each on its own line.Click to reveal answer
intermediate
Can the range in
{1..10} be changed to count backwards?Yes. You can write
{10..1} to count down from 10 to 1 in the loop.Click to reveal answer
intermediate
What happens if you use
for i in {1..10..2} in bash?The loop counts from 1 to 10 in steps of 2, so
i takes values 1, 3, 5, 7, 9.Click to reveal answer
advanced
Why is
for i in {1..10} preferred over using seq 1 10 in some cases?Because
{1..10} is a bash brace expansion that is faster and does not require calling an external command like seq.Click to reveal answer
What will the following bash code print?<br>
for i in {1..5}; do echo $((i * 2)); done✗ Incorrect
The loop runs from 1 to 5. Each number is multiplied by 2 and printed.
How do you write a for loop in bash to count down from 5 to 1?
✗ Incorrect
Brace expansion {5..1} counts down from 5 to 1.
What does the '..' operator do in
{1..10} in bash?✗ Incorrect
Brace expansion with '..' generates a sequence of numbers.
Which of these is a valid way to loop over numbers 1 to 10 in bash?
✗ Incorrect
Only brace expansion {1..10} is valid syntax for this.
What will
for i in {1..10..3} do?✗ Incorrect
The third number is the step size in brace expansion.
Explain how to use a for loop with a range in bash to print numbers 1 to 10.
Think about how to write a loop that repeats 10 times with numbers from 1 to 10.
You got /4 concepts.
Describe how to modify the range in a bash for loop to count backwards or use steps.
Consider how the numbers inside braces control the loop sequence.
You got /3 concepts.