Courseiva
Automation, Orchestration, and ScriptingmediumMultiple SelectObjective-mapped

XK0-006 Automation, Orchestration, and Scripting Practice Question

A Linux administrator is writing a bash script to process a list of usernames stored in an array. The script must iterate over each username and print a welcome message. Which TWO of the following loops will correctly iterate over the array 'users' and print 'Welcome, <username>'? (Choose TWO.)

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=0; i<${#users[@]}; i++)); do echo "Welcome, ${users[$i]}"; done

Arrays can be iterated using for with ${users[@]} or using indices. The correct methods are direct iteration over elements and using a C-style for loop with indices.

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 user in $users; do echo "Welcome, $user"; done

    Why it's wrong here

    $users expands only to the first element of the array.

  • for user in "${users[*]}"; do echo "Welcome, $user"; done

    Why it's wrong here

    ${users[*]} expands to a single string with all elements joined by IFS, so the loop runs only once.

  • while read user; do echo "Welcome, $user"; done < <(printf '%s\n' "${users[@]}")

    Why it's wrong here

    This is unnecessarily complex and not a simple loop for array iteration; it uses process substitution.

  • for ((i=0; i<${#users[@]}; i++)); do echo "Welcome, ${users[$i]}"; done

    Why this is correct

    Correct. Uses C-style for loop with array length and indexing.

  • for user in ${users[@]}; do echo "Welcome, $user"; done

    Why this is correct

    Correct. ${users[@]} expands to all elements.

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.