exit

Exit the shell.

Source: src/execution/builtins.f90:248-327

Synopsis

exit [n]

Description

Terminates the shell with exit status n. If n is omitted, the exit status is that of the last command executed. If n is not a valid integer, the exit status is 2.

Before exiting, fortsh executes any EXIT trap that has been registered with trap.

Arguments

ArgumentDescription
nExit status (integer). Optional — defaults to $?

Behavior

  1. If an EXIT trap is set, it runs first (exactly once)
  2. The shell's main loop terminates
  3. The process exits with the given status code
exit          # Exit with last command's status
exit 0        # Exit successfully
exit 1        # Exit with failure
exit 42       # Exit with custom code

Exit Status

StatusCondition
nThe provided exit code
$?Last command's status, if no argument given
2Non-numeric argument

Examples

# Exit on error
if [[ ! -f config ]]; then
    echo "Config missing" >&2
    exit 1
fi

# Cleanup before exit using trap
cleanup() {
    rm -f "$tmpfile"
}
trap cleanup EXIT

tmpfile=$(mktemp)
# Work with tmpfile...
exit 0    # cleanup runs automatically

See Also

  • trap - Set signal and exit handlers
  • return - Return from a function or sourced script