exit

shell builtinLinux/Unix
The exit command is one of the most frequently used commands in Linux/Unix-like operating systems. exit Exit the shell with a status code

Quick Reference

Command Name:

exit

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

exit [options] [arguments]

Common Use Cases

  • 1

    Script termination

    End script execution with a specific status code

  • 2

    Error handling

    Exit scripts when errors occur to prevent further processing

  • 3

    Shell termination

    Close the current shell session

  • 4

    Conditional termination

    Exit scripts based on specific conditions or user input

Syntax

exit [n]

Options

Option Description
n Exit with status code n. If n is omitted, the exit status is that of the last command executed.

Examples

How to Use These Examples

The examples below show common ways to use the exit command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

# Basic Examples Basic
exit
Exit the shell with the status of the last command.
exit 0
Exit the shell with a success status (0).
# Advanced Examples Advanced
exit 1 Exit the shell with an error status (1). if [ $? -ne 0 ]; then exit 2; fi Exit with code 2 if the previous command failed. function cleanup() { echo "Cleaning up..."; } trap cleanup EXIT exit Register a cleanup function to run when exiting.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

The exit command is a shell built-in that terminates the current shell or script with a specified status code. If no status code is provided, it uses the exit status of the most recently executed command. Exit status codes are important in shell scripting for error handling and determining the success or failure of commands and scripts. By convention: - 0 indicates success - Non-zero values (1-255) indicate various error conditions When used in an interactive shell session, exit will close the current shell. When used in a script, it will terminate the script's execution and return control to the calling program or shell with the specified status code. The exit status can be accessed in shell scripts using the special parameter $?. This makes it possible to check the result of the previous command and take appropriate action. For more sophisticated exit handling, the exit command is often used with the trap command, which allows you to specify commands to be executed when the shell receives specific signals or when the script exits.

Tips & Tricks

1

Use exit 0 to indicate successful completion

2

Use non-zero exit codes (1-255) to indicate different error conditions

3

Check the exit status of the previous command with $?

4

Use trap to register cleanup functions that run on exit

5

In functions, consider using return instead of exit to avoid terminating the entire script

Common Use Cases

Script termination

End script execution with a specific status code

Error handling

Exit scripts when errors occur to prevent further processing

Shell termination

Close the current shell session

Conditional termination

Exit scripts based on specific conditions or user input

Program status reporting

Communicate success or failure to parent processes

Related Commands

These commands are frequently used alongside exit or serve similar purposes:

Use Cases

1

Script termination

End script execution with a specific status code

2

Error handling

Exit scripts when errors occur to prevent further processing

3

Shell termination

Close the current shell session

4

Conditional termination

Exit scripts based on specific conditions or user input

5

Program status reporting

Communicate success or failure to parent processes

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the exit command works in different scenarios.

$ exit
View All Commands