What is the Exit Command?
The exit command is used to exit or close the current Linux terminal session.
It safely closes the current shell without making any changes to files or directories.
Syntax:
exit
Explanation:
- If you are working in a directory or any subdirectory, typing
exitends the terminal session. - The
$(normal-user) or#(root-user) symbol shows your current user in the terminal, but it disappears when you run theexitcommand and end the session. - Nothing in your files or folders changes; it only safely closes the session.
Example 1: Exit the terminal
Command:
$ exit
Explanation:
- After pressing Enter, the terminal closes, and you are out of the shell.
Exit Status:
It is a number that Linux gives after a command finishes running. This number shows whether the command worked or not. The operating system reads this number to know the result of the command.
The exit status value is between 0 and 255. A value of 0 means the command ran successfully without any error. Any value other than 0 means the command returned an error, a warning, or another execution result.
Linux saves the exit status of the last command in the special shell variable $?. You can check this variable to see the result of the previous command. Shell scripts also use this value to check command results and run the next command based on the result of the previous command. This makes it easy to know whether a command was successful or not.
| Exit Status | Result | Meaning |
|---|---|---|
| 0 | Success | The command shows it ran successfully and no error occurred. |
| 1 | General Error | The command shows a general error occurred. |
| 2 | Incorrect Command | The command shows it was entered with the wrong syntax or an invalid option. |
| 126 | Permission Denied | The command shows you do not have permission to run it. |
| 127 | Command Not Found | The command shows Linux could not find the command. |
| 130 | Command Interrupted | The command shows it was stopped by pressing Ctrl + C. |
| 255 | Unknown Error | The command shows an unknown or unexpected error occurred. |












