📖 Scenario: You are creating a simple countdown timer script in Bash. This script will count down from a starting number to zero, showing each number on the screen.
🎯 Goal: Build a Bash script that uses a while loop to count down from a given number to zero, printing each number.
📋 What You'll Learn
Create a variable called count with the starting number 5
Create a variable called limit with the value 0
Use a while loop that runs as long as count is greater than or equal to limit
Inside the loop, print the current value of count
Decrease count by 1 in each loop iteration
💡 Why This Matters
🌍 Real World
Counting down or up is common in scripts that wait for a certain time or repeat tasks a fixed number of times.
💼 Career
Understanding loops and variables in Bash is essential for automating tasks in system administration and DevOps roles.
Progress0 / 4 steps
1
Set up the starting number
Create a variable called count and set it to 5.
Bash Scripting
Hint
Use count=5 to create the variable.
2
Set the limit for the countdown
Create a variable called limit and set it to 0.
Bash Scripting
Hint
Use limit=0 to create the variable.
3
Write the while loop to count down
Write a while loop that runs while count is greater than or equal to limit. Inside the loop, print the value of count and then decrease count by 1.
Bash Scripting
Hint
Use while [ "$count" -ge "$limit" ] to start the loop. Use echo "$count" to print. Use count=$((count - 1)) to decrease.
4
Run the script and show the countdown
Run the script to print the countdown from 5 to 0.
Bash Scripting
Hint
Run the script in your terminal. It should print numbers from 5 down to 0, one per line.
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
Step 1: Understand the purpose of a while loop
A while loop runs repeatedly while its condition remains true.
Step 2: Compare options with the definition
Only Repeats commands as long as a condition is true correctly describes this behavior.
Final Answer:
Repeats commands as long as a condition is true -> Option D
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
Step 1: Recall bash while loop syntax
The correct syntax uses square brackets for condition and 'do' keyword.
Step 2: Match options with correct syntax
while [ condition ]; do matches the correct bash syntax: while [ condition ]; do.
Final Answer:
while [ condition ]; do -> Option A
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
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.
Step 2: Trace the output for each iteration
It prints 1, then 2, then 3, then stops because count becomes 4.
Final Answer:
1 2 3 -> Option B
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
The while loop must have 'do' after the condition to start the loop body.
Step 2: Identify missing 'do'
The script misses 'do' after the condition line, causing syntax error.
Final Answer:
Missing 'do' keyword after while condition -> Option C
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
Step 1: Understand the goal
We want to print even numbers from 2 to 10 inclusive.
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).
Step 3: Choose the best option
Only the script using modulo to explicitly check evenness prints exactly 2,4,6,8,10.
Final Answer:
num=1
while [ $num -le 10 ]; do
if [ $((num % 2)) -eq 0 ]; then echo $num; fi
num=$((num + 1))
done -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.