Bird
0
0

Which of the following is the correct way to read a file line by line in bash?

easy📝 Syntax Q3 of 15
Bash Scripting - Text Processing in Scripts
Which of the following is the correct way to read a file line by line in bash?
Afor line in filename.txt; do echo $line; done
Bwhile read line; do echo $line; done < filename.txt
Ccat filename.txt | for line in; do echo $line; done
Dread filename.txt | while line; do echo $line; done
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct bash syntax for reading lines

    The syntax 'while read line; do ... done < filename' correctly reads each line from the file.
  2. Step 2: Check other options for errors

    Options A, C, and D have syntax errors or misuse commands.
  3. Final Answer:

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

    Correct line reading syntax = B [OK]
Quick Trick: Use 'while read' loop with input redirection to read lines [OK]
Common Mistakes:
MISTAKES
  • Using for loop directly on filename
  • Piping cat into for loop incorrectly
  • Misplacing read command syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes