LPIC-1 Shells, Scripting and Data Management Practice Question
A script needs to read a file line by line, preserving leading and trailing whitespace and backslashes. Which loop construct should be used?
⚠ Common exam trap
The trap here is assuming that the -r option alone preserves all characters, overlooking that IFS still causes leading and trailing whitespace to be stripped.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
while IFS= read -r line; do echo "$line"; done < file.txt
Reading a file line by line while preserving exact content requires disabling both word splitting and backslash processing. Clearing IFS stops the shell from trimming leading and trailing whitespace, and the -r option stops backslash escapes from being interpreted. Using only one of these settings leaves the other behavior active and alters the data.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
while IFS= read -r line; do echo "$line"; done < file.txt
Why this is correct
Setting IFS to an empty value prevents read from trimming leading and trailing whitespace, and the -r option disables backslash escape processing. Together they preserve the line exactly as it appears in the file, satisfying the requirement to keep whitespace and backslashes intact during iteration.
- ✗
while read line; do echo "$line"; done < file.txt
Why it's wrong here
The default read behavior strips leading and trailing IFS whitespace and interprets backslashes as escape characters. This would alter lines that begin or end with spaces or contain backslashes, so it does not preserve the exact content required by the scenario.
- ✗
while read -r line; do echo "$line"; done < file.txt
Why it's wrong here
The -r option correctly prevents backslash interpretation, but without clearing IFS the read builtin still strips leading and trailing IFS whitespace. Lines that begin or end with spaces or tabs would be modified, so this does not fully meet the requirement to preserve whitespace.
- ✗
for line in $(cat file.txt); do echo "$line"; done
Why it's wrong here
This approach performs word splitting on the entire file contents, so lines with spaces are broken into multiple words and the loop iterates over words rather than lines. It also loses empty lines and any original line boundaries, making it unsuitable for reading a file line by line.
Go deeper
Related to this question
About these practice questions
Courseiva writes every LPIC-1 question from scratch — 402 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official LPI exam blueprint
This LPIC-1 practice question is part of Courseiva's free LPI certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the LPIC-1 exam.