- Unix Commands Reference
- Unix Commands - Home
exit Command in Linux
exit is a Linux command that allows you to terminate the current shell session on your system. When you simply type the exit command and press the Enter button, it will close the currently opened terminal window. The exit command also accepts an optional numeric argument N, which specifies the exit status. If N is not provided, the exit status of the last executed command is used.
The exit command is pretty useful for scripting where you can use it to signal the end of a script and return a status code to the calling process.
Syntax for exit Command in Linux
The syntax to use the exit command in Linux is straightforward, which is provided below −
exit[N]
Where, exit is the command itself, while [N] is an optional numeric argument representing the exit status. If this option is not utilized, then the exit status of the last executed command is used.
Examples of exit Command in Linux
Let’s explore a few examples of exit command on Linux systems −
Basic Exit
If you simply execute the exit command on Linux, it will close the current shell session −
exit
Exit with Status Code
You can also specify an exit status code to indicate whether the script or command succeeded or failed. For example, for successful execution, you can use −
exit 0
For general error, you can use −
exit 1
Exit with Last Command Status
If you don’t provide any status, the exit will use the status of the last executed command.
exit
Using exit in Scripts
You can also use the exit command in a shell script to terminate your script and return a status code to the calling process. For example −
#!/bin/bash echo "Script is running..." exit 0 # The script is successfully completed
These examples will help you in managing shell sessions and scripts effectively.
Conclusion
The exit command on Linux is useful for terminating shell sessions and scripts efficiently. We have explored the basic syntax of exit command in this guide along with its various usage on Linux systems.
Whether you are closing a terminal window or signaling the end of a script with a specific status code, you must learn the art of using the exit command. Doing this will help you manage your Linux environment more effectively.