Challenge - 5 Problems
Line Reader Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this script reading a file line by line?
Consider a file named
What will be the output of this script?
colors.txt with the following content:red green blue
What will be the output of this script?
while read line; do echo "$line" done < colors.txt
Bash Scripting
while read line; do echo "$line" done < colors.txt
Attempts:
2 left
💡 Hint
Think about how
read reads each line and how echo prints it.✗ Incorrect
The script reads each line from the file and prints it on its own line. Each echo adds a newline, so the output ends with a newline.
💻 Command Output
intermediate2:00remaining
What happens if you use a pipe with while read loop?
Given the same
colors.txt file, what will this script output?cat colors.txt | while read line; do echo "$line" done
Bash Scripting
cat colors.txt | while read line; do echo "$line" done
Attempts:
2 left
💡 Hint
Pipes send the file content to the loop line by line.
✗ Incorrect
The pipe sends the file content line by line to the loop, which prints each line.
💻 Command Output
advanced2:00remaining
What error occurs with this incorrect while read syntax?
What error or output results from running this script?
while read line echo "$line" done < colors.txt
Bash Scripting
while read line echo "$line" done < colors.txt
Attempts:
2 left
💡 Hint
Check the syntax for the while loop in bash.
✗ Incorrect
The 'do' keyword is required after the while condition to start the loop body.
🚀 Application
advanced2:00remaining
How to count lines in a file using while read loop?
Which script correctly counts the number of lines in
colors.txt using a while read loop?Attempts:
2 left
💡 Hint
Use arithmetic expansion $(( )) to increment variables in bash.
✗ Incorrect
Only option C correctly increments the count variable using $((count + 1)). Others have syntax errors or do not update count properly.
🧠 Conceptual
expert2:00remaining
Why does this while read loop skip the last line sometimes?
Consider this script:
If
while read line; do echo "$line" done < file.txt
If
file.txt does not end with a newline character, what happens to the last line and why?Attempts:
2 left
💡 Hint
Think about how the read command detects line endings.
✗ Incorrect
The read command reads until a newline. If the last line lacks a newline, read may not process it, causing it to be skipped.