Bird
0
0

Which of the following is the correct command to read a file named example.txt line by line in Linux shell?

easy📝 Syntax Q3 of 15
Linux CLI - Viewing and Editing Files
Which of the following is the correct command to read a file named example.txt line by line in Linux shell?
Acat example.txt | while read line; echo $line done
Bwhile read line; do echo $line; done < example.txt
Cread line < example.txt; echo $line
Dfor line in example.txt; do echo $line; done
Step-by-Step Solution
Solution:
  1. Step 1: Understand shell syntax for reading lines

    The correct syntax uses a while loop with input redirection to read each line.
  2. Step 2: Verify command correctness

    while read line; do echo $line; done < example.txt correctly reads each line and echoes it; others have syntax errors or misuse commands.
  3. Final Answer:

    while read line; do echo $line; done < example.txt -> Option B
  4. Quick Check:

    Correct shell loop syntax = while read line; do echo $line; done < example.txt [OK]
Quick Trick: Use while read loop with input redirection to read lines [OK]
Common Mistakes:
MISTAKES
  • Misplacing pipe and loop syntax
  • Using for loop incorrectly on file
  • Missing 'do' or 'done' keywords

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Linux CLI Quizzes