Bird
0
0

Why is it important to check the exit code $? immediately after a command in bash scripts?

hard🧠 Conceptual Q10 of 15
Bash Scripting - Error Handling
Why is it important to check the exit code $? immediately after a command in bash scripts?
ABecause $? changes after every command executed
BBecause $? stores the process ID of the last command
CBecause $? holds the number of arguments passed
DBecause $? is only set once per script
Step-by-Step Solution
Solution:
  1. Step 1: Understand how $? works

    $? stores the exit code of the last executed command only.
  2. Step 2: Explain why timing matters

    Running any other command changes $?, so checking it immediately is necessary to get the correct exit code.
  3. Final Answer:

    Because $? changes after every command executed -> Option A
  4. Quick Check:

    Check $? immediately to avoid overwriting [OK]
Quick Trick: Check $? right after command; it changes with each command [OK]
Common Mistakes:
MISTAKES
  • Thinking $? holds PID or argument count
  • Assuming $? is constant

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes