exit
Terminate a shell or script
TLDR
Exit with the exit status of the most recently executed command
Exit with a specific exit status
SYNOPSIS
exit [n]
PARAMETERS
n
An integer representing the exit status code. If omitted, the exit status of the last executed command is used.
DESCRIPTION
The `exit` command terminates the shell or script currently being executed. It can optionally return an exit status code to the calling process. If no status is provided, the `exit` command returns the exit status of the last executed command. This allows for scripts to signal success or failure to other scripts or programs. Understanding how `exit` statuses work is crucial for writing robust shell scripts that can be used in automated workflows. An exit status of 0 typically indicates success, while any non-zero value indicates an error. Be aware that the valid range for exit statuses is typically 0-255. Values outside this range might be truncated by the shell, leading to unexpected results. The exit status is a key element in managing errors and reporting process outcomes in a Linux environment. It is an important part of automation.
CAVEATS
Exit statuses are usually between 0 and 255. Passing a value outside of that range might be truncated or have unexpected behavior. Different shells may have slightly different behaviors.
EXIT STATUS CODES
A return value of 0 generally means that all went well. Any return value other than zero indicates an error. Different programs have different codes for different failures.
EXAMPLES
exit 0
indicates successful completion.exit 1
indicates a generic error.
exit $?
will exit with the return code of the previous command.exit 127
often means command not found.
HISTORY
The `exit` command has been a fundamental part of Unix-like operating systems since their inception. It's purpose remains unchanged - to provide a way for processes to signal their completion status. The concept is deeply ingrained in the shell and scripting environment.