Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Why loops repeat tasks efficiently in Bash Scripting - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a simple for loop
What is the output of this bash script?
Bash Scripting
for i in 1 2 3; do echo "Number $i"; done
ANumber 1 Number 2 Number 3
B
Number 1
Number 2
Number 3
C
1
2
3
D
Number 1
Number 2
Attempts:
2 left
💡 Hint
Look at how the echo command prints each item on a new line.
💻 Command Output
intermediate
2:00remaining
While loop counting down
What will this bash script output?
Bash Scripting
count=3
while [ $count -gt 0 ]; do
echo "Counting $count"
count=$((count - 1))
done
A
Counting 3
Counting 2
Counting 1
B
Counting 3
Counting 2
Counting 1
Counting 0
C
Counting 1
Counting 2
Counting 3
D
Counting 3
Counting 2
Attempts:
2 left
💡 Hint
The loop runs while count is greater than zero.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this for loop
Which option shows the correct syntax for a bash for loop that prints numbers 1 to 3?
Bash Scripting
for i in 1 2 3 do
 echo $i
 done
Afor i in 1 2 3; do echo $i; done
Bfor i in (1 2 3) do echo $i done
Cfor i in 1 2 3 do echo $i done
Dfor i 1 2 3; do echo $i; done
Attempts:
2 left
💡 Hint
Remember the semicolon before 'do' is required.
🔧 Debug
advanced
2:00remaining
Why does this while loop run infinitely?
What is the reason this bash script runs forever?
Bash Scripting
count=1
while [ $count -le 3 ]
do
echo $count
done
AThe condition uses '-le' which is invalid in bash.
BThe while loop syntax is incorrect; it needs a semicolon before 'do'.
CThe echo command is missing quotes.
DThe variable 'count' is never updated inside the loop.
Attempts:
2 left
💡 Hint
Check if the loop changes the variable controlling the condition.
🚀 Application
expert
3:00remaining
Create a loop to list all .txt files with line counts
Which bash script correctly lists all '.txt' files in the current folder and prints their line counts?
Afor file in *.txt; do wc -l "$file"; done
Bls *.txt | while read file; do echo "$file: $(wc -l < "$file") lines"; done
Cfor file in *.txt; do echo "$file: $(wc -l < "$file") lines"; done
Dfor file in *.txt; do echo "$file: wc -l $file"; done
Attempts:
2 left
💡 Hint
Use command substitution and redirect wc input to avoid extra filename in output.

Practice

(1/5)
1. Why do loops help in scripting tasks?
for i in 1 2 3; do echo $i; done
easy
A. They only work with numbers
B. They make scripts run slower
C. They repeat tasks automatically to save time and effort
D. They stop the script after one task

Solution

  1. Step 1: Understand the loop purpose

    The loop repeats the echo command for each number in the list automatically.
  2. Step 2: Recognize efficiency

    Repeating tasks with loops saves time and effort compared to writing commands multiple times.
  3. Final Answer:

    They repeat tasks automatically to save time and effort -> Option C
  4. Quick Check:

    Loops repeat tasks = They repeat tasks automatically to save time and effort [OK]
Hint: Loops automate repeated tasks to save effort [OK]
Common Mistakes:
  • Thinking loops slow down scripts
  • Believing loops stop after one run
  • Assuming loops only work with numbers
2. Which of these is the correct syntax for a basic for loop in bash?
easy
A. for (i=1; i<=3; i++) { echo $i }
B. for i in 1 2 3; do echo $i; done
C. loop i from 1 to 3 echo $i
D. foreach i in 1 2 3 echo $i

Solution

  1. Step 1: Identify bash loop syntax

    Bash uses 'for variable in list; do commands; done' format.
  2. Step 2: Compare options

    Only for i in 1 2 3; do echo $i; done matches bash syntax; others use other languages or incorrect forms.
  3. Final Answer:

    for i in 1 2 3; do echo $i; done -> Option B
  4. Quick Check:

    Bash for loop syntax = for i in 1 2 3; do echo $i; done [OK]
Hint: Bash loops use 'for var in list; do ...; done' [OK]
Common Mistakes:
  • Using C-style loops in bash
  • Using 'foreach' which is not bash syntax
  • Missing 'do' and 'done' keywords
3. What will be the output of this script?
for i in 1 2 3; do echo "Number $i"; done
medium
A. Number 1 Number 2 Number 3
B. Number 1 Number 2 Number 3
C. Number 123
D. Syntax Error

Solution

  1. Step 1: Understand the loop iterations

    The loop runs three times, each time echoing 'Number' plus the current number.
  2. Step 2: Recognize echo output format

    Each echo prints on a new line, so output has three lines with 'Number 1', 'Number 2', 'Number 3'.
  3. Final Answer:

    Number 1\nNumber 2\nNumber 3 -> Option A
  4. Quick Check:

    Loop prints each item on new line = Number 1 Number 2 Number 3 [OK]
Hint: Echo prints each loop item on a new line [OK]
Common Mistakes:
  • Thinking echo outputs on same line without newline
  • Confusing concatenated output with separate lines
  • Expecting syntax error from correct code
4. Find the error in this loop:
for i in 1 2 3
 echo $i
done
medium
A. Missing 'do' keyword after for statement
B. Wrong variable name used
C. Extra 'done' keyword
D. Loop should use while instead of for

Solution

  1. Step 1: Check loop syntax

    Bash for loops require 'do' after the for line before commands.
  2. Step 2: Identify missing keyword

    The code misses 'do' after 'for i in 1 2 3', causing syntax error.
  3. Final Answer:

    Missing 'do' keyword after for statement -> Option A
  4. Quick Check:

    For loop needs 'do' keyword = Missing 'do' keyword after for statement [OK]
Hint: Always add 'do' after for statement in bash loops [OK]
Common Mistakes:
  • Forgetting 'do' keyword
  • Thinking 'done' is extra
  • Confusing variable names
5. You want to print all filenames in a folder using a loop. Which script efficiently does this?
1) for file in *; do echo $file; done
2) for file in folder/*; echo $file; done
3) while read file; do echo $file; done < folder
4) for file in folder; do echo $file; done
hard
A. Option 3
B. Option 2
C. Option 4
D. Option 1

Solution

  1. Step 1: Understand globbing for files

    The pattern '*' matches all files in the current directory, so 'for file in *' loops over all filenames.
  2. Step 2: Check each option

    Option 1 correctly loops over all files and echoes them. Option 2 misses 'do'. Option 3 reads lines from folder as a file, not filenames. Option 4 loops over the string 'folder' only.
  3. Final Answer:

    Option 1 -> Option D
  4. Quick Check:

    Use 'for file in *; do ...; done' to loop files [OK]
Hint: Use 'for file in *; do ...; done' to loop files [OK]
Common Mistakes:
  • Forgetting 'do' keyword in loops
  • Using folder name as string instead of glob
  • Confusing reading file contents with 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