Courseiva

XK0-006 Automation, Orchestration, and Scripting Practice Question

A bash script needs to test whether a string variable $NAME is non-empty and equals 'admin'. Which of the following conditionals is correct?

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

✓

if [[ -n $NAME && $NAME == 'admin' ]]; then

In bash, [[ -n $NAME ]] tests if the string is non-empty, and == compares strings. Using double brackets is safer for string comparison.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    if [[ $NAME -ne '' && $NAME -eq 'admin' ]]; then

    Why it's wrong here

    The -ne and -eq operators perform integer comparison, so bash treats 'admin' as an arithmetic operand and errors rather than matching the string. It tempts because -ne reads naturally as 'not equal', and these operators are correct when both operands are genuinely numeric values being compared arithmetically.

  • ✓

    if [[ -n $NAME && $NAME == 'admin' ]]; then

    Why this is correct

    The -n test confirms the string is non-empty, and && requires the equality check to also pass. Both conditions must hold for the branch to execute, precisely matching the stem's requirement that $NAME be populated and equal to 'admin'.

  • ✗

    if [ $NAME != '' ] && [ $NAME == 'admin' ]; then

    Why it's wrong here

    Unquoted $NAME expands to nothing when empty, leaving [ != '' ], which bash rejects as a syntax error before the second test runs. It tempts because the logic is right and quoting is easy to overlook; this form works correctly whenever the variable is guaranteed non-empty at evaluation time.

  • ✗

    if [ ! -z $NAME -a $NAME = 'admin' ]; then

    Why it's wrong here

    Using -a in [ ] is valid but less robust; also -z tests empty, ! -z is non-empty. However, = is for string comparison in [ ]. This is technically correct but not as idiomatic as A.

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 →

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.