Linux exit Command Explained for Beginners (With Examples)
If you are a Linux newbie, and your work involves doing stuff through the command line, then it goes without saying that you must be spending a lot of time on the terminal window. As you'd likely agree, there are some commands that we tend to use very frequently, like ls, cp, and rm. However, there are some others that are comparatively less used. In this tutorial, we will discuss one such, less frequently used command: Exit.
Please note that all examples and instructions mentioned in this article have been tested on Bash shell running on Ubuntu 16.04LTS.
Linux exit command
The exit command lets you quit the shell where it's run.
If your shell window has multiple tabs, then this command exits the tab where it's executed. Given that it's a built-in command, it's highly likely that you won't find a dedicated man page for exit. To access its official documentation, execute the following command:
On my system, the aforementioned command produced the following output:
exit: exit [n]
Exit the shell.
Exits the shell with a status of N. If N is omitted, the exit status
is that of the last command executed.
Now, some of you may ask why (or rather when) is N required. Well, the value - which is basically exit status - is used mostly when the the command is used from within a script (bash script). In some cases, this value is mapped to a human-readable error, warning, or notification.
Also, as clear from the help command output above, if the value N is not explicitly specified, the exit status of the last executed command is considered to be that value.
Let's take a simple example: What I did was, I switched user accounts and entered the root shell:
Then, I exited the shell using the exit command. Also, I used 8 as the exit status value.
Now, in the parent shell (where I returned), I used the following command to check the exit status:
So you can see it's the same status value that was passed to the exit command in the root shell. So this way we can access the status, and then do whatever that's intended.
Now, here's another example where-in I didn't explicitly pass any exit status from the root shell, but upon inquiring in the parent shell, the exit status of last command that was run in the root shell was returned:
If you want, you can also define some actions that you want the system to take when a shell exits. For example, you might want to remove one or multiple files upon exit. For this, you have to set a trap, which you can do in the following way:
trap "ENTER-COMMAND-HERE" EXIT
So whatever action you want the system to take upon exit, you need to specify the corresponding command in double quotes above. For example, I used the following command:
trap "rm hypotheticalfile.txt" EXIT
The fact is that no such file exists on my system, so after the exit command is executed, the shell should display an error for this. And that's what actually happened - see below:
So this way, you can set traps on exit. For more information on traps, run the following command:
There isn't much of a learning curve when it comes to the exit command, especially if you are a command line beginner. And you'd likely agree with this now. So just quickly try out what all we've discussed here, and start using the exit command (if you don't do that already). In case of any doubt or query, leave a comment below.