Courseiva
easyMultiple ChoiceObjective-mapped

XK0-006 Practice Question: The backup script above always outputs 'Backup…

Exhibit

Refer to the exhibit.

Exhibit:
#!/bin/bash
# Script to backup /var/www
tar -czf /backup/www-$(date +%F).tar.gz /var/www
if [ &? -eq 0 ]; then
    echo "Backup successful"
else
    echo "Backup failed"
fi

The backup script above always outputs 'Backup failed' even when the tar command succeeds. Which of the following is the cause?

⚠ Common exam trap

CompTIA often tests the distinction between `$?` and common typos like `&?` or `?$`, exploiting the fact that candidates may overlook the exact syntax of shell special variables and assume any symbol before `?` works.

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

The correct variable is $? not &?

The script uses `&?` to reference the exit status of the `tar` command, but the correct shell variable is `$?`. The `$?` variable holds the exit code of the last executed command (0 for success, non-zero for failure). Using `&?` is a syntax error that results in an empty or invalid value, causing the `if` statement to always evaluate to false (or treat the condition as non-zero), thus always printing 'Backup failed'.

Answer analysis

Option-by-option breakdown

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

  • The tar command should use -czvf

    Why it's wrong here

    Verbose flag does not affect exit status.

  • The if statement syntax is wrong

    Why it's wrong here

    The if-then-else syntax is correct; the issue is the variable name.

  • The variable &? is not defined

    Why it's wrong here

    While true that &? is not a standard variable, the specific cause is that $? should be used.

  • The correct variable is $? not &?

    Why this is correct

    The script incorrectly uses &? instead of the correct $? variable.

Visual reference

Client Server SYN (seq=100) SYN-ACK (seq=200, ack=101) ACK (ack=201) Connection established — data transfer begins

About these practice questions

This XK0-006 question is part of Courseiva's 979-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.