Courseiva
Automation, Orchestration, and ScriptingmediumMultiple ChoiceObjective-mapped

XK0-006 for loop Practice Question

A system administrator is writing a Bash script that needs to iterate over a list of IP addresses stored in a file, one per line. Which loop construct should be used?

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

for i in $(cat ip_list.txt); do ... done

The for loop with command substitution ($(cat ip_list.txt)) iterates over each item in the file output, making it the appropriate construct for iterating over a list of IP addresses. Option B uses C-style syntax incorrectly and would not iterate over the file contents directly. Option C uses a while-read loop, which is intended for reading lines from input, not for iterating over a list of items from a file; therefore, it is not the loop construct asked for. Option D uses an until loop that requires a conditional expression, not suitable for direct list iteration from a 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.

  • for i in $(cat ip_list.txt); do ... done

    Why this is correct

    Correct. `for i in $(cat ip_list.txt); do ... done` iterates over each line from the output of `cat`, treating it as a list item. This works well when the file contains one IP per line without spaces.

  • for ((i=0; i<$(wc -l < ip_list.txt); i++)); do ... done

    Why it's wrong here

    Incorrect. This arithmetic `for` loop uses a counter based on `wc -l`, but it does not directly iterate over the file contents. Additional commands would be needed to access each line, making it less efficient and not the standard approach.

  • while read line; do ... done < ip_list.txt

    Why it's wrong here

    Incorrect. `while read line; do ... done < ip_list.txt` is a valid way to read a file line by line, but it is a `while` loop, not a `for` loop. The question expects a `for` loop for iterating over a list, so this is not the best answer. However, in practice, `while read` is often more robust for handling lines with spaces or special characters.

  • until IFS= read -r line; do ... done < ip_list.txt

    Why it's wrong here

    Incorrect. The `until` loop is similar to `while`, but the syntax here is flawed (`IFS=` without `read -r` inside the condition). Even if corrected, it is not the typical loop for this task.

About these practice questions

One of 979 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 by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

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.