LX0-104 · Question #255
Which of the following commands will print the exit value of the previous command to the screen in bash?
The correct answer is A. echo $?. In bash, the special variable $? holds the exit status of the most recently executed foreground command.
Question
Options
- Aecho $?
- Becho $#
- Cecho $exit
- Decho $status
- Eecho $&}
How the community answered
(27 responses)- A96% (26)
- D4% (1)
Why each option
In bash, the special variable `$?` holds the exit status of the most recently executed foreground command.
The special shell variable `$?` contains the exit status of the last executed foreground command, which `echo` will print to standard output.
`$#` contains the number of positional parameters passed to a script or function, not the exit status.
`$exit` is not a standard shell variable for the exit status; `exit` is a command.
`$status` is used in some shells like csh/tcsh for the exit status, but not in bash.
`$&}}` is not a valid or recognized shell variable or construct.
Concept tested: Shell special variables (exit status)
Source: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Special-Parameters
Topics
Community Discussion
No community discussion yet for this question.