The exit command is a common control instruction provided by many command-line shells and programming language interpreters. Its primary role is to terminate the current process, end a script or session, and return an integer status code to the parent process. Because it directly stops execution, exit is used both to signal success or failure and to ensure an environment is left in a known state.

Behavior and characteristics

When invoked, exit ends the current shell or interpreter instance. Typical behaviors include:

  • Accepting an optional numeric argument that becomes the exit status. By convention, 0 indicates success and nonzero values indicate errors or specific conditions.
  • If no argument is given, many shells return the status of the last executed command as the exit status.
  • Running any registered cleanup handlers or trap routines (for example, signal traps in POSIX shells) before the process actually terminates.
  • In interactive use, exiting a login shell will log the user out or close the terminal emulator; in scripts, it stops the script and returns control to the caller.

Common uses and examples

exit is used in a variety of contexts: ending a user session, terminating a script when an error occurs, or returning specific codes from utilities. Example forms (written here as plain text):

  • Shell: exit 0 # indicate success
  • Shell: exit 2 # indicate an error condition
  • Script idiom: test_condition || exit 1 # stop on failure

Many interpreted languages offer an equivalent. For brief language references see general documentation, and language-specific pages such as Perl and PHP, which provide built-in exit functions that end execution and return an integer status to the operating system.

History and variants

The concept of an exit or termination instruction traces to early command interpreters and operating-system process models where a child process must indicate completion to its parent. Different systems and shells implement small variations: some environments provide separate logout commands for login shells, some batch systems use variants to return from a script without closing the entire console, and many programming languages include synonyms (for example, die in some languages) that also stop execution.

Distinctions and practical notes

exit should not be confused with forcibly killing other processes (for which system signals or job-control commands are used), nor with returning from a function within a program. When used responsibly, exit is a simple and predictable mechanism to end execution and communicate a concise status to whatever invoked the process.