nerdexam
LPI

102-500 · Question #173

When the command echo $ outputs 1, which of the following statements is true?

The correct answer is C. It is the exit value of the command executed immediately before echo. In bash, $? is a special variable that holds the exit status of the most recently completed command - so when echo $? outputs 1, that 1 was set by whatever command ran just before echo, making C correct. Option A is wrong because $? has nothing to do with process IDs; $$…

Shells and Shell Scripting

Question

When the command echo $ outputs 1, which of the following statements is true?

Options

  • AIt is the process ID of the echo command.
  • BIt is the process ID of the current shell.
  • CIt is the exit value of the command executed immediately before echo.
  • DIt is the exit value of the echo command.

How the community answered

(31 responses)
  • A
    10% (3)
  • B
    3% (1)
  • C
    71% (22)
  • D
    16% (5)

Explanation

In bash, $? is a special variable that holds the exit status of the most recently completed command - so when echo $? outputs 1, that 1 was set by whatever command ran just before echo, making C correct. Option A is wrong because $? has nothing to do with process IDs; $$ (double dollar) is the variable for the current shell's PID. Option B is wrong for the same reason - $$ stores the shell's PID, not $?. Option D is wrong because echo itself typically exits with 0 (success); by the time echo finishes, its own exit status would overwrite $?, but we're reading $? as the argument to echo, which captures the prior command's status before echo runs.

Memory tip: Think of ? as the shell asking "how did that go?" - it stores the answer from the last command (0 = success, non-zero = failure), and it gets refreshed after every command completes.

Topics

#Exit status#Special variables#$? variable#Shell fundamentals

Community Discussion

No community discussion yet for this question.

Full 102-500 Practice