Immediately after a command, save its numeric exit status.
Which special parameter should be referenced?
$? contains the previous pipeline's exit status and must be saved before another command overwrites it.
Detailed explanation
$$Incorrect. $$ is the current shell PID.
Incorrect. $$ is the current shell PID.
$#Incorrect. $# is the positional-argument count.
Incorrect. $# is the positional-argument count.
$?Correct. $? is the exit status of the preceding command or pipeline.
Correct. $? is the exit status of the preceding command or pipeline.
$!Incorrect. $! is the most recent asynchronous command's PID.
Incorrect. $! is the most recent asynchronous command's PID.
Try it yourself
An example you can run in a temporary verification environment.
false; status=$?; printf '%s\n' "$status"Expected result
1Key points
- 0 means success
- Nonzero indicates failure or another condition
- Save immediately
Notes
- Environment: GNU Bash 5.2 / Bash組込みfalse
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.