Courseiva
Create simple shell scriptsmediumMultiple ChoiceObjective-mapped

EX200 Create simple shell scripts Practice Question

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?

⚠ Common exam trap

In Red Hat Enterprise Linux shell scripting, a common mistake is to use `set -e` or conditional operators like `&&` when the goal is to capture the exit status regardless of success or failure. The correct approach is to assign `$?` unconditionally immediately after the command.

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

./risky_command; rc=$?; echo $rc

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.

Answer analysis

Option-by-option breakdown

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

  • set -e; ./risky_command; rc=$?; echo $rc

    Why it's wrong here

    The errexit option (set -e) causes the shell to terminate immediately when risky_command returns a non-zero exit status, so the subsequent rc=$? line is never executed. This means a failing command aborts the entire script before the exit code can be captured, making the echo statement unreachable. Only if the command succeeds would flow continue, but then rc would be 0 and the failure case is lost.

  • rc=$? ./risky_command; echo $rc

    Why it's wrong here

    This syntax performs the variable assignment rc=$? before risky_command is invoked, so it captures the exit status of the previous command, not risky_command. Moreover, because the assignment precedes a command, it is scoped to that command's environment only; the variable is not updated in the current shell session when the command exits. Consequently, echo $rc prints the old status, which is unrelated to risky_command.

  • ./risky_command; rc=$?; echo $rc

    Why this is correct

    The semicolon is a command terminator that allows rc=$? to execute regardless of how risky_command exited. After risky_command finishes, $? holds its exact exit status, and the assignment immediately stores it before any other command or expansion can alter it. The subsequent echo $rc then prints the saved value, making this the correct pattern for capturing a failure status without terminating the script.

  • ./risky_command && rc=$?; echo $rc

    Why it's wrong here

    The && operator is a logical AND that runs the right-hand command only if the left-hand command exits with status 0. If risky_command fails with a non-zero status, rc=$? is skipped, leaving rc unset or containing a stale value from a previous statement. The final echo $rc then outputs either a blank line (if unset) or the wrong exit code, so this pattern does not reliably capture failure status.

About these practice questions

One of 127 original EX200 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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 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.