The answer is that the script uses `&?` instead of the correct `$?` variable to reference the exit status. In Bash, `$?` is the special variable that holds the exit code of the last executed command, returning 0 for success and a non-zero value for failure. The typo `&?` is not a valid shell variable, so it evaluates to an empty or meaningless value, causing the `if` condition to always be treated as false (or non-zero), which triggers the 'Backup failed' message regardless of the `tar` command's actual result. On the CompTIA Linux+ XK0-005 exam, this tests your understanding of exit codes and proper variable syntax in shell scripting—a common trap is confusing `$?` with other symbols like `&` or `%`. A reliable memory tip is to think of the dollar sign as asking for the "status" of the last command: `$?` literally means "what was the exit status?"
XK0-005 Scripting, Containers and Automation Practice Question
This XK0-005 practice question tests your understanding of scripting, containers and automation. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.
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?
Clue words in this question
Noticing these words before you look at the options changes how you read each choice.
Clue: "always"
Why it matters: Absolute qualifier. An answer using 'always' is only correct if there are genuinely no exceptions — absolute statements are often wrong in networking.
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
A
The tar command should use -czvf
Why wrong: Verbose flag does not affect exit status.
B
The if statement syntax is wrong
Why wrong: The if-then-else syntax is correct; the issue is the variable name.
C
The variable &? is not defined
Why wrong: While true that &? is not a standard variable, the specific cause is that $? should be used.
D
The correct variable is $? not &?
The script incorrectly uses &? instead of the correct $? variable.
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'.
Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
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.
Clue confirmation
The clue word "always" in the question point toward this answer.
Related concept
Read the scenario before looking for a memorised answer.
Common exam traps
Common exam trap: answer the scenario, not the keyword
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.
Detailed technical explanation
How to think about this question
In bash and POSIX shells, `$?` is a special parameter that expands to the decimal exit status of the most recently executed foreground pipeline. It is updated after every command, including simple commands, pipelines, and compound commands. A common real-world pitfall is accidentally using `$?` inside a subshell or after a command that resets it (e.g., `echo`), which can lead to incorrect exit code checks. In this script, the typo `&?` is not a valid variable reference, so the `if` condition effectively becomes `[ '' -ne 0 ]`, which is always true (empty string is not equal to 0), causing the failure branch to execute.
KKey Concepts to Remember
Read the scenario before looking for a memorised answer.
Find the constraint that changes the correct option.
Eliminate answers that are true in general but not in this case.
TExam Day Tips
→Watch for words such as best, first, most likely and least administrative effort.
→Review why wrong options are wrong, not only why the correct option is correct.
Key takeaway
Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Real-world example
How this comes up in practice
A practitioner preparing for the XK0-005 exam encounters this exact type of scenario on the job. The correct answer here is not the most general option — it is the best answer for the specific constraint described. Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option. Real exam questions reward reading the full scenario before eliminating options, because the constraint defines which answer fits.
Related glossary terms
Concepts from this question explained
These glossary pages explain the core terms tested in this XK0-005 question in full detail.
Scripting, Containers and Automation — This question tests Scripting, Containers and Automation — Read the scenario before looking for a memorised answer..
What is the correct answer to this question?
The correct answer is: 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'.
What should I do if I get this XK0-005 question wrong?
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
Are there clue words in this question I should notice?
Yes — watch for: "always". Absolute qualifier. An answer using 'always' is only correct if there are genuinely no exceptions — absolute statements are often wrong in networking.
What is the key concept behind this question?
Read the scenario before looking for a memorised answer.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
This XK0-005 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-005 exam.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.