Open In App

exit command in Linux with Examples

Last Updated : 06 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The ‘exit’ command is a fundamental command in Linux used to exit the current shell session. This command is widely used in scripting and command-line operations, allowing users and scripts to terminate a session or a process in a controlled manner. The ‘exit’ command can take an optional parameter [N], which specifies the exit status code to be returned upon exiting the shell. If no parameter is provided, the command returns the exit status of the last executed command.

Syntax:

exit [n]

Options and Examples for the ‘exit’ Command

1. Exit Without Parameters

The simplest use of the ‘exit’ command is without any parameters. When executed, it will close the current terminal session:

After pressing enter, the terminal will simply close.

2. Exit With a Specific Status Code

You can specify an exit status code when exiting a session. This is useful for scripting and error handling:

After pressing enter, the terminal window will close and return a status of 110. Return status is important because sometimes they can be mapped to tell error, warnings and notifications. For example generally, return status – “0” means the program has executed successfully. “1” means the program has minor errors.

3. Exiting a Root Session

Using “sudo su” we are going to the root directory and then exit the root directory with a return status of 5. After returning it will show how to display the return status code. ‘echo $?’ command is used to see the last return status.

4. Displaying Help Information

It displays help information.

This command will display the help section for the exit command, providing an overview of its usage and options.

Importance of Exit Status Codes

Exit status codes play a crucial role in Linux, especially in scripting and automation. They provide a way to indicate the outcome of command execution:

  • 0: Success – The command executed successfully without errors.
  • 1: General Error – The command encountered minor errors.
  • 2: Misuse of Shell Builtins – Indicates that a shell built-in command was misused.

Conclusion

The ‘exit’ command in Linux is not just a simple way to close the terminal session—it’s a powerful tool that helps manage processes and scripts by using exit status codes. You can better control the flow of scripts and improve error handling and make your automation processes more reliable and robust with the help of specific exit codes.


Next Article

Similar Reads

three90RightbarBannerImg