A C-style for loop in bash uses the syntax: for (( initialization; condition; increment )); do commands; done. It works like the for loop in C language, controlling the loop with a counter variable.
Click to reveal answer
beginner
Write the syntax of a C-style for loop in bash.
for (( i=0; i<10; i++ )); do
# commands
done
Click to reveal answer
beginner
How does the increment part work in a C-style for loop in bash?
The increment part updates the loop variable after each iteration. For example, i++ adds 1 to i each time the loop runs.
Click to reveal answer
beginner
What will this loop print?
for (( i=1; i<=3; i++ )); do echo $i; done
It will print:
1
2
3
Each number on its own line, counting from 1 to 3.
Click to reveal answer
intermediate
Can you use negative increments in a C-style for loop in bash?
Yes, you can. For example, for (( i=5; i>0; i-- )) will count down from 5 to 1.
Click to reveal answer
What is the correct syntax to start a C-style for loop in bash?
Afor i in 0 1 2 3 4
Bwhile (( i<5 ))
Cfor i=0 to 5
Dfor (( i=0; i<5; i++ ))
✗ Incorrect
The C-style for loop uses double parentheses with initialization, condition, and increment separated by semicolons.
What does the increment part in a C-style for loop do?
AChecks if the loop should continue
BUpdates the loop variable after each iteration
CInitializes the loop variable
DEnds the loop
✗ Incorrect
The increment part changes the loop variable, usually increasing or decreasing it, after each loop cycle.
Which of these loops counts down from 3 to 1?
Afor (( i=0; i<3; i++ ))
Bfor (( i=1; i<=3; i++ ))
Cfor (( i=3; i>0; i-- ))
Dfor (( i=3; i<0; i++ ))
✗ Incorrect
The loop starts at 3, continues while i>0, and decreases i by 1 each time.
What will this loop print?
for (( i=0; i<3; i++ )); do echo $i; done
A0 1 2
B1 2 3
C0 1 2 3
D1 2
✗ Incorrect
The loop prints 0, then 1, then 2, because it runs while i is less than 3.
Can you use a C-style for loop to loop over a list of strings in bash?
ANo, you must use 'for var in list' syntax
BNo, C-style loops only work with numbers
CYes, but only with arrays
DYes, directly with C-style syntax
✗ Incorrect
C-style for loops are for numeric loops. To loop over strings, use 'for var in list' syntax.
Explain how a C-style for loop works in bash scripting.
Think about how you set a counter, check it, and change it each time.
You got /5 concepts.
Write a C-style for loop in bash that counts from 10 down to 1 and prints each number.
Use decrement operator and condition i>=1.
You got /4 concepts.
Practice
(1/5)
1. What does the following C-style for loop do in bash? for ((i=1; i<=3; i++)); do echo $i; done
easy
A. Prints numbers 1, 2, and 3 each on a new line
B. Prints numbers 0, 1, and 2 each on a new line
C. Prints numbers 1, 2, 3, and 4 each on a new line
D. Causes a syntax error
Solution
Step 1: Understand the loop initialization and condition
The loop starts with i=1 and runs while i is less than or equal to 3.
Step 2: Analyze the increment and output
Each iteration prints the current value of i, then increases i by 1 until i becomes 4 and stops.
Final Answer:
Prints numbers 1, 2, and 3 each on a new line -> Option A
Quick Check:
Loop from 1 to 3 prints 1, 2, 3 [OK]
Hint: Check start, end, and increment values carefully [OK]
Common Mistakes:
Starting loop at 0 instead of 1
Confusing <= with < in condition
Forgetting to increment i
2. Which of the following is the correct syntax for a C-style for loop in bash?
easy
A. for (i=0; i<5; i++) do echo $i; done
B. for ((i=0; i<5; i++)); do echo $i; done
C. for [i=0; i<5; i++] do echo $i; done
D. for {i=0; i<5; i++} do echo $i; done
Solution
Step 1: Identify correct C-style for loop syntax in bash
Bash requires double parentheses (( )) around the loop control with semicolons separating init, condition, and increment.
Step 2: Check each option
for ((i=0; i<5; i++)); do echo $i; done uses double parentheses and semicolons correctly; others use incorrect brackets or missing parentheses.
Final Answer:
for ((i=0; i<5; i++)); do echo $i; done -> Option B
Quick Check:
Double parentheses and semicolons = correct syntax [OK]
Hint: Look for double parentheses and semicolons inside [OK]
Common Mistakes:
Using single parentheses instead of double
Using square or curly brackets
Missing semicolons between parts
3. What is the output of this bash script?
for ((i=2; i<=6; i+=2)); do echo $i; done
medium
A. 2 3 4 5 6
B. Syntax error
C. 2 4 6 8
D. 2 4 6
Solution
Step 1: Understand loop initialization and increment
The loop starts at i=2, runs while i is less than or equal to 6, and increments i by 2 each time.
Step 2: List values printed
Values printed are 2, then 4, then 6. After 6, i becomes 8 which breaks the condition.
Final Answer:
2 4 6 -> Option D
Quick Check:
Increment by 2 from 2 to 6 prints 2,4,6 [OK]
Hint: Check increment step and loop condition carefully [OK]
Common Mistakes:
Assuming increment is 1 by default
Including numbers beyond the condition
Confusing <= with <
4. Identify the error in this bash C-style for loop:
for ((i=0; i<5 i++)); do echo $i; done
medium
A. Missing semicolon between condition and increment
B. Using single parentheses instead of double
C. Incorrect variable name
D. Missing do keyword
Solution
Step 1: Check loop syntax parts
The loop control must have three parts separated by semicolons: initialization, condition, and increment.
Step 2: Locate missing semicolon
Between 'i<5' and 'i++' there is no semicolon, causing a syntax error.
Final Answer:
Missing semicolon between condition and increment -> Option A
Quick Check:
Semicolons separate parts in C-style for loops [OK]
Hint: Check for three semicolon-separated parts inside (( )) [OK]
Common Mistakes:
Omitting semicolons between parts
Using single parentheses
Forgetting do keyword
5. You want to print all odd numbers from 1 to 9 using a C-style for loop in bash. Which script achieves this?
hard
A. for ((i=1; i<10; i++)); do if (( i % 2 == 0 )); then echo $i; fi; done
B. for ((i=0; i<10; i++)); do if (( i % 2 == 0 )); then echo $i; fi; done
C. for ((i=1; i<=9; i+=2)); do echo $i; done
D. All of the above
Solution
Step 1: Analyze for ((i=1; i<=9; i+=2)); do echo $i; done
for ((i=1; i<=9; i+=2)); do echo $i; done starts at 1, increments by 2, prints 1,3,5,7,9 directly.
Step 2: Analyze options B and C
Both loop with i++ and check if (( i % 2 == 0 )), which selects even numbers: B prints 0,2,4,6,8; C prints 2,4,6,8.
Final Answer:
for ((i=1; i<=9; i+=2)); do echo $i; done -> Option C
Quick Check:
Increment by 2 from 1 to <=9 prints odds 1-9 [OK]
Hint: Use increments or modulo checks to filter odd numbers [OK]
Common Mistakes:
Using wrong increment step
Starting from 0 without filtering
Forgetting to check odd condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.