nerdexam
LPI

102-500 · Question #83

What information is shown by the echo $? command?

The correct answer is B. The exit value of the command executed immediately before echo. $? is a special shell variable that holds the exit status of the most recently executed command - a value of 0 means success, and any non-zero value indicates an error. When you run echo $?, the shell expands $? before echo runs, so what gets printed is the exit status of…

Shells and Shell Scripting

Question

What information is shown by the echo $? command?

Options

  • AThe process ID of the echo command.
  • BThe exit value of the command executed immediately before echo.
  • CThe process ID which will be used for the next command.
  • DThe exit value of the echo command.
  • EThe process ID of the current shell.

How the community answered

(51 responses)
  • A
    2% (1)
  • B
    82% (42)
  • C
    4% (2)
  • D
    10% (5)
  • E
    2% (1)

Explanation

$? is a special shell variable that holds the exit status of the most recently executed command - a value of 0 means success, and any non-zero value indicates an error. When you run echo $?, the shell expands $? before echo runs, so what gets printed is the exit status of whatever command ran before the echo.

Why the distractors are wrong:

  • A & C - $? has nothing to do with process IDs. The variable for the current shell's PID is $$, and there's no built-in variable for the "next" PID.
  • D - This is the subtle trap: by the time echo executes, $? has already been replaced with the previous command's exit value. You're not capturing echo's own status.
  • E - The current shell's PID is $$, not $?.

Memory tip: Think of $? as "what happened?" - it answers whether the last command succeeded or failed, like checking the result of the previous action before moving on.

Topics

#exit status#shell variables#$?#command execution

Community Discussion

No community discussion yet for this question.

Full 102-500 Practice