Courseiva

XK0-006 Automation, Orchestration, and Scripting Practice Question

A Linux administrator is troubleshooting a Bash script that must parse each line of a file named `servers.txt` and process fields separated by colons. The script currently uses `for line in $(cat servers.txt)` and breaks on lines containing spaces. Which construct should replace the loop to correctly iterate over lines while preserving whitespace?

⚠ Common exam trap

The trap here is piping into `while read`, which appears to work for simple output but silently loses variable changes made inside the loop.

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 ... done < servers.txt

Reading lines reliably requires disabling word splitting and backslash processing with `IFS= read -r line`, and attaching the input redirection to the `while` loop so the loop does not run in a subshell. Piping into the loop loses variable state, command substitution splits on whitespace, and placing the redirection on the `read` command reopens the file on each iteration instead of advancing through it.

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 line in $(< servers.txt); do ... done

    Why it's wrong here

    Command substitution performs word splitting on whitespace, so each whitespace-delimited token becomes a separate iteration rather than each line. This is the same defect as the original loop. Field values containing spaces would be split across multiple iterations, corrupting the parsing of colon-separated records.

  • ✓

    while IFS= read -r line; do ... done < servers.txt

    Why this is correct

    `IFS= read -r line` reads a full line without word splitting or backslash interpretation, and redirecting the file into the loop prevents the subshell problem that occurs when piping. This preserves spaces and special characters within each line. It is the standard, reliable pattern for line-by-line processing in Bash scripts.

  • ✗

    cat servers.txt | while read line; do ... done

    Why it's wrong here

    Piping into `while` runs the loop in a subshell, so any variables modified inside are lost after the loop ends. Additionally, without `IFS=` and `-r`, leading and trailing whitespace is stripped and backslashes are interpreted. This makes it unsuitable for preserving whitespace and for accumulating results across iterations.

  • ✗

    while read -r line < servers.txt; do ... done

    Why it's wrong here

    Redirecting the file inside the `while` condition reopens the file on every iteration, so only the first line is ever read repeatedly, causing an infinite loop. The file descriptor must be attached to the loop itself, not to the `read` command, for sequential reading to work correctly.

About these practice questions

One of 781 original XK0-006 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.