0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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.