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.
status=$$Incorrect. $$ is the current shell's process ID.
status=$!Incorrect. $! is the PID of the most recently started background job.
status=$?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
Shell function
A reusable group of commands that runs in the caller's shell context.
Positional parameter
A numbered argument or special value supplied to a script or function.
Control flow
Conditions, loops, and exit statuses determine which shell commands run.