LPIC-1 Shells, Scripting and Data Management Practice Question
A script contains the following line: for i in $(cat file.txt); do echo $i; done. The file file.txt contains a single line with multiple words. How many times will the loop execute?
⚠ Common exam trap
Test-takers frequently assume $(cat file.txt) preserves line boundaries, but the for loop splits the output by whitespace, so the number of iterations equals the number of words, not lines.
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
✓
Equal to the number of words in the file
The command substitution $(cat file.txt) expands to the content of file.txt, which is a single line with multiple words. The for loop iterates over each word (separated by whitespace) in the expanded string, not over lines. Therefore, the loop executes once per word in the file.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Equal to the number of lines in the file
Why it's wrong here
Command substitution splits the file's contents on whitespace, so the loop iterates once per word, not per line. It is tempting because line count matches iterations when each line holds a single word; with multiple words on one line, word count governs.
- ✓
Equal to the number of words in the file
Why this is correct
Command substitution splits on IFS whitespace, so each word from the single line becomes a separate argument to `for`. The loop therefore iterates once per word, satisfying the stem's constraint of one line containing multiple words. Word count, not line count, determines execution.
- ✗
Once
Why it's wrong here
Command substitution splits the file's contents on whitespace, so each word becomes a separate item and the loop runs once per word, not once. It is tempting because a single physical line suggests a single iteration, which would hold only if word splitting were disabled.
- ✗
The loop will not execute
Why it's wrong here
The file contains words, so command substitution yields a non-empty list and the loop body runs. It is tempting because an empty or missing file would produce zero iterations, which is the scenario where this answer would be correct.
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 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.