🚪 Command: exit (Terminate Session)
The exit command is used to close the current shell session, terminate a script, or log out of a remote connection.
1. The "Why"
In your workflow, you often layer different environments. You might be:
- Logged into your main user account on Arch Linux.
- Using
sudo -ito act as root. - Connected to a remote server via SSH.
- Running a specialized sub-shell or script.
exit is the proper way to "step back" one layer. Instead of just closing the terminal window (which can sometimes leave background processes hanging), exit tells the system to clean up and shut down the current session gracefully.
2. Basic Syntax
exit [N]
- N: An optional "exit status" number.
- 0 usually means "Success."
- Any other number (1-255) indicates an "Error" or a specific reason for stopping.
3. Practical Scenarios
A. Closing the Terminal
Typing exit in your main terminal window is the keyboard-driven way to close the application. It is the equivalent of clicking the "X" button.
B. Stepping Out of Root
If you used sudo -i or su to become the superuser, typing exit will drop you back down to your normal user account (ahmed).
[root@arch ~]# exit
[ahmed@arch ~]$
C. Ending an SSH Connection
If you are logged into another computer remotely, exit will close that connection and return you to your local prompt.
D. Within a Script
In your Bash scripts, you can use exit to stop the script early if something goes wrong.
if [ ! -f "important_file.txt" ]; then
echo "Error: File missing!"
exit 1
fi
4. The Keyboard Shortcut
Most Linux users use the universal shortcut instead of typing the word:
Ctrl + D- In a blank terminal, this sends an "EOF" (End Of File) signal, which the shell interprets as an
exitcommand. It’s the fastest way to log out or close a window.
- In a blank terminal, this sends an "EOF" (End Of File) signal, which the shell interprets as an
5. Pro-Tips
- Check the Last Exit Status: If you want to know if the last command you ran was successful (status 0) or failed, type:
echo $? - Logout vs. Exit: On some systems,
exitcloses the shell, whilelogoutis specifically for ending a login session (like at the physical console). In most modern setups, they do the same thing. - Arch Linux Context: You'll use this constantly when moving between your standard user and the root user while editing system files in
/etc/.
6. Summary Reference
| Goal | Command / Shortcut |
|---|---|
| Close current shell | exit |
| Fast logout/close | Ctrl + D |
| Exit with error code | exit 1 |
| Back to normal user | exit (from root) |