A required argument is missing, so processing cannot continue.
Return a status that lets the caller detect failure.
Which command is appropriate?
exit with a nonzero value terminates a script as a failure. The exact value should have a consistent documented meaning.
Detailed explanation
exit 0Incorrect. exit 0 reports success and would hide the argument error.
Incorrect. exit 0 reports success and would hide the argument error.
exit 2Correct. exit 2 returns a nonzero failure status.
Correct. exit 2 returns a nonzero failure status.
continue 2Incorrect. continue controls loop iterations and does not terminate the script.
Incorrect. continue controls loop iterations and does not terminate the script.
shift 2Incorrect. shift removes positional arguments and does not exit.
Incorrect. shift removes positional arguments and does not exit.
Try it yourself
An example you can run in a temporary verification environment.
sh -c 'exit 2'; printf '%s\n' "$?"Expected result
2Key points
- Nonzero means failure
- The caller receives the exit value
- Document meanings consistently
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.