LPIC-1 Shells, Scripting and Data Management Practice Question
A senior administrator runs a script that processes a CSV file. The script contains the following snippet: 'for field in $(cat data.csv); do ...'. The data.csv file contains lines like: 'John Doe, 123 Main St, Springfield'. The script fails to process correctly, splitting fields incorrectly and causing errors. Which of the following is the most appropriate fix?
⚠ Common exam trap
Many candidates think quoting the command substitution or using `xargs` will fix the splitting issue, but they fail to recognize that `for` inherently splits on IFS, whereas `while read` processes one line at a time without word splitting.
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
✓
Use a while loop with read: 'while IFS= read -r line; do ... done < data.csv'
The script fails due to word splitting and globbing when iterating over the output of `cat data.csv` with a `for` loop. Using `while IFS= read -r line` reads each line verbatim, preserving spaces and commas, and is the standard pattern for processing CSV or delimited files line by line in bash.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use xargs to process each line
Why it's wrong here
xargs splits input on whitespace, so it would still break 'John Doe' and '123 Main St' into separate arguments, reproducing the same field-splitting fault. It suits passing many discrete arguments to a command, not parsing delimited records with embedded spaces.
- ✗
Use 'for field in $(<data.csv)' with proper quoting
Why it's wrong here
$(<data.csv) only replaces cat with a faster builtin read; unquoted it still undergoes word-splitting on spaces, so 'John Doe' and '123 Main St' fragment identically. Proper quoting would merge all lines into one field, not parse records.
- ✗
Use 'for field in "$(cat data.csv)"' with double quotes around the substitution
Why it's wrong here
Quoting the command substitution collapses the entire file into one string, so the loop runs once with all lines concatenated. It fixes word-splitting for single values, not for iterating CSV records; IFS and read are needed to split on commas.
- ✓
Use a while loop with read: 'while IFS= read -r line; do ... done < data.csv'
Why this is correct
Unquoted command substitution splits on IFS whitespace, so 'John Doe, 123 Main St' fragments into separate words rather than one record. A while loop with IFS= and read -r preserves each line intact, correctly handling embedded spaces and commas in the CSV.
Go deeper
Related to this question
About these practice questions
This LPIC-1 question is part of Courseiva's 402-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
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.