📖 Scenario: You have a text file that contains a list of your favorite fruits, one fruit per line. You want to read this file line by line and print each fruit with a friendly message.
🎯 Goal: Build a bash script that reads a file called fruits.txt line by line using while read loop and prints each fruit with the message: "I like <fruit>".
📋 What You'll Learn
Create a file named fruits.txt with the exact fruits listed.
Create a variable to hold the filename.
Use a while read loop to read the file line by line.
Print each fruit with the message: "I like <fruit>".
💡 Why This Matters
🌍 Real World
Reading files line by line is common when processing logs, configuration files, or lists in scripts.
💼 Career
Many automation and system administration tasks require reading files line by line to handle data or perform actions.
Progress0 / 4 steps
1
Create the fruits.txt file
Create a file named fruits.txt with these exact lines: Apple Banana Cherry
Bash Scripting
Hint
Use cat with a here document to create the file.
2
Set the filename variable
Create a variable called filename and set it to fruits.txt.
Bash Scripting
Hint
Use filename="fruits.txt" to assign the variable.
3
Read the file line by line
Use a while read loop with variable fruit to read each line from $filename.
Bash Scripting
Hint
Use while read fruit; do ... done < "$filename" to read lines.
4
Print the output
Run the script and print the output lines exactly as: I like Apple I like Banana I like Cherry
Bash Scripting
Hint
Just run the script to see the output.
Practice
(1/5)
1. What does the command while IFS= read -r line; do echo "$line"; done < file.txt do in a bash script?
easy
A. Deletes the contents of file.txt.
B. Reads the entire file at once and prints it.
C. Writes each line of file.txt to a new file.
D. Reads the file file.txt line by line and prints each line.
Solution
Step 1: Understand the while read loop
The loop reads input line by line from the redirected file file.txt.
Step 2: Check the action inside the loop
Each line read is stored in variable line and printed with echo.
Final Answer:
Reads the file file.txt line by line and prints each line. -> Option D
Quick Check:
Line-by-line reading = A [OK]
Hint: Remember: while read loops read files line by line [OK]
Common Mistakes:
Thinking it reads the whole file at once
Confusing input redirection with output
Ignoring the role of IFS= and -r
2. Which of the following is the correct syntax to read a file line by line in bash?
easy
A. while read line; do echo $line; done < file.txt
B. while read -r line; do echo $line; done > file.txt
C. while read line; do echo $line; done >> file.txt
D. while read line; do echo $line; done | file.txt
Solution
Step 1: Identify input redirection for reading
The correct way to read from a file is using input redirection: < file.txt.
Step 2: Check the loop syntax
while read line; do ... done is the standard loop to read lines.
Final Answer:
while read line; do echo $line; done < file.txt -> Option A
Quick Check:
Input redirection with < = C [OK]
Hint: Use < file.txt to feed file lines into the loop [OK]
Common Mistakes:
Using output redirection (>) instead of input (<)
Piping file name instead of redirecting input
Missing the -r option to prevent backslash escapes
3. What will be the output of this script if file.txt contains:
apple
banana
cherry
Script:
while IFS= read -r line; do echo "Fruit: $line"; done < file.txt
medium
A. Fruit: apple
Fruit: banana
Fruit: cherry
B. apple
banana
cherry
C. Fruit: apple banana cherry
D. Syntax error
Solution
Step 1: Understand the loop reading each line
The loop reads each line from file.txt separately.
Step 2: Check the echo command
Each line is printed prefixed with "Fruit: ".
Final Answer:
Fruit: apple
Fruit: banana
Fruit: cherry -> Option A
Quick Check:
Line prefixing with echo = B [OK]
Hint: Each loop iteration prints one line with prefix [OK]
Common Mistakes:
Expecting all lines printed on one line
Ignoring the prefix in echo
Confusing input and output redirection
4. Identify the error in this script snippet:
while read line; do echo $line; done file.txt
medium
A. Echo command syntax is wrong
B. Incorrect variable name used
C. Missing input redirection operator < before file.txt
D. Missing semicolon after read
Solution
Step 1: Check how the file is provided to the loop
The file must be redirected as input using < file.txt, not as an argument.
Step 2: Confirm syntax correctness
The rest of the syntax is correct; variable and echo usage are fine.
Final Answer:
Missing input redirection operator < before file.txt -> Option C
Quick Check:
Input redirection needed = D [OK]
Hint: Use < file.txt to feed file lines, not argument [OK]
Common Mistakes:
Passing filename as argument instead of redirecting input
Forgetting semicolon after read (optional here)
Misusing echo syntax
5. You want to read a file line by line and skip empty lines. Which script snippet correctly does this?
hard
A. while read line; do echo "$line"; done < file.txt | grep '^$'
B. while IFS= read -r line; do if [[ -n $line ]]; then echo "$line"; fi; done < file.txt
C. cat file.txt | while read line; do echo "$line"; done
D. while IFS= read -r line; do echo "$line"; done < file.txt
Solution
Step 1: Understand skipping empty lines
We need to check if the line is not empty using [[ -n $line ]].
Step 2: Confirm reading and condition
while IFS= read -r line; do if [[ -n $line ]]; then echo "$line"; fi; done < file.txt reads lines safely and prints only non-empty lines inside the loop.
Final Answer:
while IFS= read -r line; do if [[ -n $line ]]; then echo "$line"; fi; done < file.txt -> Option B
Quick Check:
Condition inside loop filters empty lines = A [OK]
Hint: Use if [[ -n $line ]] inside loop to skip empty lines [OK]
Common Mistakes:
Using external grep after loop (may lose variable scope)
Not checking for empty lines inside the loop
Using unnecessary cat command
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.