XK0-006 Automation, Orchestration, and Scripting Practice Question
A Linux administrator is writing a Bash script that processes a list of filenames stored one per line in a file. Some filenames contain spaces. The administrator wants the loop to treat each full line as a single item without word-splitting, and also wants to strip the trailing newline from each item. Which construct accomplishes this?
⚠ Common exam trap
The trap here is fixing the read command but forgetting to quote the variable at the point of use, which re-splits the line anyway.
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 process "$line"; done < filelist
Reading lines safely in Bash requires neutralizing both whitespace trimming and escape processing. IFS= preserves the line exactly, and -r stops backslashes from acting as escapes. Quoting the variable at every use prevents a second round of word splitting. Only the construct that combines all three elements keeps filenames with spaces or special characters as single, unmodified 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.
- ✗
while read line; do process "$line"; done < filelist
Why it's wrong here
Without IFS=, read strips leading and trailing IFS whitespace from the line, which silently alters filenames that begin or end with spaces. Omitting -r also lets backslashes act as escape characters, mangling paths containing backslashes. The loop reads lines correctly in most cases, but the missing options make it unreliable for arbitrary filenames.
- ✗
while IFS= read -r line; do process $line; done < filelist
Why it's wrong here
The read portion is correct, but calling process $line without quotes reintroduces word splitting at the point of use. A filename such as my report.txt arrives at process as two arguments, defeating the purpose of preserving the line. The variable must be quoted as "$line" to pass the whole string as one argument.
- ✓
while IFS= read -r line; do process "$line"; done < filelist
Why this is correct
Setting IFS= for the read command disables trimming of leading and trailing whitespace, and the -r flag prevents backslashes from being interpreted as escapes. read consumes one line at a time and strips the terminating newline, so filenames with embedded spaces stay intact and are passed quoted to process. This is the canonical safe line-reading loop.
- ✗
for line in $(cat filelist); do process "$line"; done
Why it's wrong here
Command substitution followed by an unquoted for loop subjects the output to word splitting on IFS, so any filename containing spaces is broken into multiple items. Globbing can also expand wildcard characters in filenames. This classic anti-pattern destroys exactly the filenames the administrator must preserve, so it fails the requirement.
Go deeper
Related to this question
Learn chapter
File Transfer and Remote Access
Key term
Linux
Linux is an open-source operating system that manages computer hardware and software, widely used in servers, desktops, and embedded systems.
Key term
Bash
Bash is a command-line interpreter that lets users interact with an operating system by typing text commands instead of clicking icons.
About these practice questions
This XK0-006 question is part of Courseiva's 781-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 and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official CompTIA exam blueprint
This XK0-006 practice question is part of Courseiva's free CompTIA 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 XK0-006 exam.