Run check_data.
Immediately save its exit status in status without running another command first.
Which assignment is appropriate?
$? contains the exit status of the most recently completed command or pipeline. Running another command before reading it overwrites the value.
Detailed explanation
status=$#Incorrect. $# is the number of positional arguments.
Incorrect. $# is the number of positional arguments.
status=$$Incorrect. $$ is the current shell's process ID.
Incorrect. $$ is the current shell's process ID.
status=$!Incorrect. $! is the PID of the most recently started background job.
Incorrect. $! is the PID of the most recently started background job.
status=$?Correct. status=$? saves check_data's exit status immediately.
Correct. status=$? saves check_data's exit status immediately.
Try it yourself
An example you can run in a temporary verification environment.
sh -c 'false; status=$?; printf "%s\n" "$status"'Expected result
1Key points
- $? is the exit status
- Read it immediately
- 0 means success and nonzero means failure
Notes
- Environment: POSIX sh / 状態変更なし
- 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.