Bird
Raised Fist0
Bash Scriptingscripting~20 mins

while loop in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
While Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a simple while loop counting down
What is the output of this Bash script?
Bash Scripting
count=3
while [ $count -gt 0 ]
do
  echo $count
  count=$((count - 1))
done
A
3
2
1
B
3
2
1
0
C
1
2
3
D
0
1
2
3
Attempts:
2 left
💡 Hint
Remember the loop runs while count is greater than zero and decreases count each time.
💻 Command Output
intermediate
2:00remaining
While loop with a break condition
What will this Bash script output?
Bash Scripting
i=1
while true
 do
  echo $i
  if [ $i -eq 3 ]; then
    break
  fi
  i=$((i + 1))
done
A
1
2
B
1
2
3
4
C
1
2
3
D
1
2
3
4
5
Attempts:
2 left
💡 Hint
The loop breaks when i equals 3.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this while loop
Which option shows the correct syntax to fix the error in this Bash while loop?
Bash Scripting
count=5
while [ $count -gt 0 ]
  echo $count
  count=$((count - 1))
done
A
Add 'do' after the while condition:
while [ $count -gt 0 ]; do
BReplace 'done' with 'end'
CRemove the square brackets from the condition
DAdd a semicolon after 'done'
Attempts:
2 left
💡 Hint
A while loop in Bash requires a 'do' keyword after the condition.
🔧 Debug
advanced
2:00remaining
Why does this while loop run infinitely?
This script runs forever. What is the reason?
Bash Scripting
num=1
while [ $num -le 3 ]
do
  echo $num
done
AThe echo command causes the loop to restart
BVariable 'num' is never updated inside the loop
CMissing 'do' keyword after while condition
DThe condition uses '-le' which is invalid
Attempts:
2 left
💡 Hint
Check if the loop changes the variable controlling the condition.
🚀 Application
expert
3:00remaining
Create a while loop to read lines from a file
Which script correctly reads each line from 'input.txt' and prints it?
A
while read line; do
  echo $line
  done > input.txt
B
while [ read line ]; do
  echo $line
done < input.txt
C
while read line
  echo $line
done < input.txt
D
while read line; do
  echo "$line"
done < input.txt
Attempts:
2 left
💡 Hint
The 'read' command reads a line, and the loop must redirect input from the file.

Practice

(1/5)
1. What does a while loop do in a bash script?
easy
A. Stops the script immediately
B. Runs commands only once
C. Executes commands only if a condition is false
D. Repeats commands as long as a condition is true

Solution

  1. Step 1: Understand the purpose of a while loop

    A while loop runs repeatedly while its condition remains true.
  2. Step 2: Compare options with the definition

    Only Repeats commands as long as a condition is true correctly describes this behavior.
  3. Final Answer:

    Repeats commands as long as a condition is true -> Option D
  4. Quick Check:

    while loop = repeat while true [OK]
Hint: While loops run repeatedly while condition is true [OK]
Common Mistakes:
  • Thinking while loops run only once
  • Confusing while with if statements
  • Assuming it runs when condition is false
2. Which of the following is the correct syntax to start a while loop in bash?
easy
A. while [ condition ]; do
B. while ( condition ) {
C. while condition then
D. while { condition } do

Solution

  1. Step 1: Recall bash while loop syntax

    The correct syntax uses square brackets for condition and 'do' keyword.
  2. Step 2: Match options with correct syntax

    while [ condition ]; do matches the correct bash syntax: while [ condition ]; do.
  3. Final Answer:

    while [ condition ]; do -> Option A
  4. Quick Check:

    Correct bash while syntax = while [ condition ]; do [OK]
Hint: Use square brackets and 'do' to start while loop [OK]
Common Mistakes:
  • Using parentheses instead of square brackets
  • Missing 'do' keyword
  • Using curly braces incorrectly
3. What will be the output of this script?
count=1
while [ $count -le 3 ]; do
  echo $count
  count=$((count + 1))
done
medium
A. 1 2 3 4
B. 1 2 3
C. 3 2 1
D. No output

Solution

  1. Step 1: Analyze the loop condition and increments

    The loop starts with count=1 and runs while count is less or equal to 3, incrementing count by 1 each time.
  2. Step 2: Trace the output for each iteration

    It prints 1, then 2, then 3, then stops because count becomes 4.
  3. Final Answer:

    1 2 3 -> Option B
  4. Quick Check:

    Loop prints 1 to 3 [OK]
Hint: Count increments until condition false, prints each value [OK]
Common Mistakes:
  • Forgetting to increment count causing infinite loop
  • Expecting output beyond 3
  • Confusing loop condition operators
4. Identify the error in this script:
count=1
while [ $count -lt 5 ]
  echo $count
  count=$((count + 1))
done
medium
A. Wrong comparison operator '-lt'
B. Incorrect variable increment syntax
C. Missing 'do' keyword after while condition
D. Missing closing bracket in condition

Solution

  1. Step 1: Check while loop syntax

    The while loop must have 'do' after the condition to start the loop body.
  2. Step 2: Identify missing 'do'

    The script misses 'do' after the condition line, causing syntax error.
  3. Final Answer:

    Missing 'do' keyword after while condition -> Option C
  4. Quick Check:

    while loop needs 'do' [OK]
Hint: Always put 'do' after while condition [OK]
Common Mistakes:
  • Omitting 'do' keyword
  • Misplacing brackets
  • Incorrect increment syntax
5. You want to write a bash script that prints all even numbers from 2 to 10 using a while loop. Which script correctly does this?
hard
A. num=1 while [ $num -le 10 ]; do if [ $((num % 2)) -eq 0 ]; then echo $num; fi num=$((num + 1)) done
B. num=2 while [ $num -le 10 ]; do echo $num num=$((num + 1)) done
C. num=2 while [ $num -lt 10 ]; do echo $num num=$((num + 2)) done
D. num=0 while [ $num -le 10 ]; do echo $num num=$((num + 2)) done

Solution

  1. Step 1: Understand the goal

    We want to print even numbers from 2 to 10 inclusive.
  2. Step 2: Check each option's logic

    num=2 while [ $num -le 10 ]; do echo $num num=$((num + 1)) done starts at 2 and increments by 1 up to 10, printing 2 3 4 5 6 7 8 9 10 - incorrect.
    num=1 while [ $num -le 10 ]; do if [ $((num % 2)) -eq 0 ]; then echo $num; fi num=$((num + 1)) done starts at 1, increments by 1, and prints only if even - correct.
    num=2 while [ $num -lt 10 ]; do echo $num num=$((num + 2)) done stops before 10, missing 10.
    num=0 while [ $num -le 10 ]; do echo $num num=$((num + 2)) done starts at 0, which is even but outside requested range (2 to 10).
  3. Step 3: Choose the best option

    Only the script using modulo to explicitly check evenness prints exactly 2,4,6,8,10.
  4. Final Answer:

    num=1 while [ $num -le 10 ]; do if [ $((num % 2)) -eq 0 ]; then echo $num; fi num=$((num + 1)) done -> Option A
  5. Quick Check:

    Check even with modulo inside loop [OK]
Hint: Use modulo inside loop to print even numbers [OK]
Common Mistakes:
  • Stopping loop before 10
  • Starting from 0 when 2 is required
  • Not checking evenness explicitly
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