LFCS · 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 $? This question asks for the correct Bash command to display the exit status of the previously executed command.
Question
Options
- Aecho $?
- Becho $#
- Cecho $exit
- Decho $status
- Eecho $&}
How the community answered
(31 responses)- A87% (27)
- B3% (1)
- C3% (1)
- D6% (2)
Why each option
This question asks for the correct Bash command to display the exit status of the previously executed command.
In Bash, the special parameter `$?` holds the exit status of the last executed foreground command. An exit status of `0` generally indicates success, while any non-zero value signifies an error or abnormal termination, making `echo $?` the correct way to display it.
`echo $#` prints the number of positional parameters passed to a script or function, not the exit status of the last command.
`echo $exit` is incorrect because `exit` is a command used to terminate a shell, not a variable storing the exit status.
`echo $status` is not a standard Bash variable for exit status; `status` is used in some other shells like `csh` or `tcsh`.
`echo $&}}` is not valid Bash syntax for retrieving the exit status of a command.
Concept tested: Bash exit status variable `$?`
Source: https://www.gnu.org/software/bash/manual/bash.html#Special-Parameters
Topics
Community Discussion
No community discussion yet for this question.