$? Variable: Exit Status Overwritten by [[ Conditional
This EX200 practice question tests your understanding of create simple shell scripts. The scenario asks you to isolate a root cause — eliminate options that address a different problem before choosing. 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.
```bash
#!/bin/bash
# Script to test a condition
if [[ $? -eq 0 ]]; then
echo 'Success'
fi
```
A developer runs the script shown in the exhibit and always sees 'Success' printed, even when the previous command fails. What is the most likely cause?
Clue words in this question
Noticing these words before you look at the options changes how you read each choice.
Clue: "most likely"
Why it matters: Probability qualifier — the question wants the most probable cause or outcome, not a guaranteed one. Eliminate low-probability options.
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.
Exhibit
Refer to the exhibit.
```bash
#!/bin/bash
# Script to test a condition
if [[ $? -eq 0 ]]; then
echo 'Success'
fi
```
A
The [[ ]] syntax always evaluates to true
Why wrong: A is incorrect because [[ ]] evaluates conditionally; it only returns true if the condition is true, not always.
B
The $? variable is only set after external commands, not builtins
Why wrong: B is incorrect because $? is updated after every command, including builtins and keywords.
C
The $? variable captures the exit status of the [[ command, not the intended command
C is correct because $? captures the exit status of the [[ ]] construct itself, not the previous intended command.
D
The $? variable always returns 0 in a conditional
Why wrong: D is incorrect because $? reflects the exit status of the last command, which can be non-zero if the condition is false.
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
The $? variable captures the exit status of the [[ command, not the intended command
Option C is correct because the `[[ ]]` conditional construct is a shell keyword that itself produces an exit status. When `$?` is checked immediately after `[[ ]]`, it captures the exit status of the `[[ ]]` evaluation (which is 0 if the condition is true, 1 if false), not the exit status of the command that was run before the `[[ ]]`. Since the developer always sees 'Success' printed, the `[[ ]]` condition must be evaluating to true (exit status 0), causing `$?` to be 0 and the script to always take the success path, regardless of the actual previous command's result.
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 [[ ]] syntax always evaluates to true
Why it's wrong here
A is incorrect because [[ ]] evaluates conditionally; it only returns true if the condition is true, not always.
✗
The $? variable is only set after external commands, not builtins
Why it's wrong here
B is incorrect because $? is updated after every command, including builtins and keywords.
✓
The $? variable captures the exit status of the [[ command, not the intended command
Why this is correct
C is correct because $? captures the exit status of the [[ ]] construct itself, not the previous intended command.
Clue confirmation
The clue words "most likely", "always" in the question point toward this answer.
Related concept
Read the scenario before looking for a memorised answer.
✗
The $? variable always returns 0 in a conditional
Why it's wrong here
D is incorrect because $? reflects the exit status of the last command, which can be non-zero if the condition is false.
Common exam traps
Common exam trap: answer the scenario, not the keyword
The trap here is that candidates mistakenly think `$?` always reflects the original command's exit status, not realizing that `[[ ]]` is itself a command that resets `$?` to its own exit status, causing the check to always succeed if the `[[ ]]` expression is syntactically valid.
Trap categories for this question
Keyword trap
B is incorrect because $? is updated after every command, including builtins and keywords.
Command / output trap
B is incorrect because $? is updated after every command, including builtins and keywords.
Detailed technical explanation
How to think about this question
In bash, `$?` holds the exit status of the last foreground pipeline. When `[[ condition ]]` is used, it is a compound command that returns 0 for true and 1 for false. If a developer writes `if [[ $? -eq 0 ]]; then ... fi`, the `$?` inside the `[[ ]]` refers to the exit status of the command that ran before the `if`, but the `[[ ]]` itself then overwrites `$?` with its own exit status. A common real-world scenario is using `$?` inside a `[[ ]]` test without saving it first, leading to logic errors where the condition always succeeds because `[[ ]]` returns 0 for a valid expression.
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 EX200 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.
What to study next
Got this wrong? Here's your next step.
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
Create simple shell scripts — This question tests Create simple shell scripts — Read the scenario before looking for a memorised answer..
What is the correct answer to this question?
The correct answer is: The $? variable captures the exit status of the [[ command, not the intended command — Option C is correct because the `[[ ]]` conditional construct is a shell keyword that itself produces an exit status. When `$?` is checked immediately after `[[ ]]`, it captures the exit status of the `[[ ]]` evaluation (which is 0 if the condition is true, 1 if false), not the exit status of the command that was run before the `[[ ]]`. Since the developer always sees 'Success' printed, the `[[ ]]` condition must be evaluating to true (exit status 0), causing `$?` to be 0 and the script to always take the success path, regardless of the actual previous command's result.
What should I do if I get this EX200 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: "most likely", "always". Probability qualifier — the question wants the most probable cause or outcome, not a guaranteed one. Eliminate low-probability options.
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 →
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A script needs to execute a command that might fail, but the script should continue. The administrator wants to capture the exit status for logging. Which code snippet correctly implements this?
medium
A.set -e; ./risky_command; rc=$?; echo $rc
B.rc=$? ./risky_command; echo $rc
✓ C../risky_command; rc=$?; echo $rc
D../risky_command && rc=$?; echo $rc
Why C: Option C is correct because it runs the risky command, captures its exit status immediately after in the `$?` variable, and then echoes it for logging. The script continues regardless of the command's success or failure, which meets the requirement. The `$?` variable holds the exit status of the last executed foreground command, so assigning it to `rc` right after `./risky_command` ensures the correct value is stored.
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 EX200 practice question is part of Courseiva's free Red Hat 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 EX200 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.