Bird
0
0

You wrote this script snippet:

medium📝 Debug Q14 of 15
Bash Scripting - Variables
You wrote this script snippet:
echo "Last command status: $?"
ls /nonexistent

But it always prints Last command status: 0 even if the directory doesn't exist. What is the problem?
AThe <code>ls</code> command is correct, no problem
BThe <code>echo</code> runs before <code>ls</code>, so $? is from previous command
CYou should use <code>$$</code> instead of <code>$?</code>
DYou need to quote <code>$?</code> differently
Step-by-Step Solution
Solution:
  1. Step 1: Understand when $? is evaluated

    $? holds the exit status of the last command executed before it.
  2. Step 2: Check command order

    The echo runs first, so it shows the status of the command before echo, not ls.
  3. Final Answer:

    The echo runs before ls, so $? is from previous command -> Option B
  4. Quick Check:

    Check command order for $? [OK]
Quick Trick: Print $? immediately after the command to get correct status [OK]
Common Mistakes:
MISTAKES
  • Using $$ instead of $? for exit status
  • Printing $? before running the command
  • Assuming $? holds script PID

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes