LFCS · Question #5
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. The special shell variable $? stores the exit status (return code) of the most recently executed foreground command.
Question
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
(34 responses)- A3% (1)
- B3% (1)
- C88% (30)
- D6% (2)
Why each option
The special shell variable `$?` stores the exit status (return code) of the most recently executed foreground command.
The `$?` variable does not represent the process ID of any command; `$$` is used for the current shell's PID, and `$!` for the last background command's PID.
The `$?` variable is not used to represent the process ID of the current shell; that is the purpose of the `$$` variable.
The `$?` variable is a special parameter in shell scripting that holds the exit status of the command that was executed immediately before it. A non-zero value like 1 typically indicates that the preceding command encountered an error or a specific condition.
The `$?` variable stores the exit status of the *previous* command, not the `echo` command itself. The `echo` command would generally return 0 upon successful execution.
Concept tested: Shell special variable `$?` (exit status)
Source: https://www.gnu.org/software/bash/manual/bash.html#Special-Parameters
Topics
Community Discussion
No community discussion yet for this question.