EX200 Create simple shell scripts Practice Question
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?
⚠ Common exam trap
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.
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 $? variable captures the exit status of the [[ command, not the intended command
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.
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
The [[ ]] compound command is a conditional expression that evaluates the test inside; it returns 0 only when the test is true, and 1 when false. Its outcome is always dependent on the operands and operators (e.g., -f, -z, =~, <, >). Thus claiming it always evaluates to true is incorrect; it can just as easily evaluate to false and yield a non-zero exit status.
- ✗
The $? variable is only set after external commands, not builtins
Why it's wrong here
The shell sets $? after every command, including builtins like echo or cd, keywords like [[, and functions. It is not limited to external binaries; the shell updates it after each pipeline or simple command. Therefore, a builtin such as [[ will update $? when it runs.
- ✓
The $? variable captures the exit status of the [[ command, not the intended command
Why this is correct
In the given script, if an intended command is followed by a [[ ... ]] test, $? will reflect the exit status of the [[ ]] evaluation, not the earlier command. For example, if the script does [[ -f file ]] after running an application, $? becomes 1 when file does not exist, even if the application succeeded. Because $? is overwritten by each subsequent command, any intervening conditional destroys the original exit value.
- ✗
The $? variable always returns 0 in a conditional
Why it's wrong here
The value of $? is simply the exit code of the last executed command; a conditional does not force it to be 0. When a [[ ]] test is false, it returns 1 (or another non-zero code), so $? becomes non-zero. Thus $? will be 0 only if the last command happened to succeed, not because it was evaluated in a conditional context.
Go deeper
Related to this question
About these practice questions
Courseiva writes every EX200 question from scratch — 127 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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 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.