When arguments are invalid, terminate the entire script immediately with status 2.
Which statement is appropriate?
exit N terminates the current shell script with status N; return and break have narrower control scopes.
Detailed explanation
return 2Incorrect. return exits a function or sourced file rather than generally terminating the top-level script.
break 2Incorrect. break leaves a loop and does not end the whole script with status 2.
exit 2Correct. exit 2 terminates the script with the requested status.
shift 2Incorrect. shift removes positional parameters and does not terminate execution.
Try it yourself
An example you can run in a temporary verification environment.
bash --noprofile --norc -c 'exit 2'; status=$?; printf '%s\n' "$status"Expected result
2Key points
- exit terminates the shell
- Numeric exit status
- return is for functions or sourced files
Notes
- Environment: GNU Bash 5.2 / 短命シェル
- 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.