LPIC-1 Shells, Scripting and Data Management Practice Question
When writing a Bash script, which two constructs can be used to safely iterate over a list of filenames that may contain spaces or special characters? (Choose TWO)
⚠ Common exam trap
Many candidates assume command substitution (`$(...)`) or simple glob expansion safely handles filenames with spaces, but the shell performs word splitting and glob expansion on the unquoted result, leading to broken loops or security issues.
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
✓
find . -name '*.txt' -exec echo {} \;
The `-exec` action in `find` passes each filename as a separate argument to the command, avoiding word splitting and glob expansion. This ensures that filenames containing spaces, tabs, or newlines are handled safely without being broken into multiple arguments.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
for file in *.txt; do ... done
Why it's wrong here
Subject to word splitting; filenames with spaces would be split.
- ✓
find . -name '*.txt' -exec echo {} \;
Why this is correct
Executes a command per file without shell word splitting.
- ✗
for file in $(find . -name '*.txt'); do ... done
Why it's wrong here
Subject to word splitting and pathname expansion of the output.
- ✓
while IFS= read -r file; do ... done < <(find . -name '*.txt' -print0)
Why this is correct
Uses NUL delimiter to safely handle any characters.
- ✗
for file in "*.txt"; do ... done
Why it's wrong here
Treats the pattern as a literal string, not a glob.
Go deeper
Related to this question
About these practice questions
Courseiva writes every LPIC-1 question from scratch — 527 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.