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"
fiThe 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
Go deeper
Related to this question
Learn chapter
Basic Shell Scripting
Key term
Value
Value is the perceived worth, benefit, or usefulness that a service, product, or activity delivers to stakeholders, especially customers and the business.
Key term
Shell
A shell is a computer program that provides a user interface to access an operating system's services, typically by accepting text commands.
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 →
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.