Linux exit command

Linux exit command is used to exit from the current shell. It takes a parameter as a number and exits the shell with a return of status number. If we did not provide any parameter, it would return the status of the last executed command. The exit command closes a script and exits the shell.

If we have more than one shell tab, the exit command will close the tab where it is executed. This is a built-in command, and we cannot find a dedicated manual page for this.

Syntax:

From the above command, after pressing the ENTER key, the terminal will be closed, and all the normal running processes of the terminal will be ended.

The exit command is the most useful common in Linux. We can pass with it many times. It uses the exit() function to terminate the normal process. Some points regarding the exit command are as following:

  • It is used to exit from the shell script with an exit status N.
  • It can be used to verify whether the shell script is successfully terminated or not.
  • The exit status can be used by other commands to take their actions.
  • If the value of N is not specified, the exit status will be the last executed command.
  • The value of N is set to 0, and it stands for the normal shell exit.

Options:

The exit command does not provide many options. But, it supports the following options:

  • Exit without any parameter:

If we execute it without any parameter, it simply closes the terminal. Execute it as follows:


Linux exit command

The above command will simply close the terminal.

  • Exit with exit status:

If we pass a parameter, it will close and return the same exit status. For example, if we execute it with an exit status 10, it will return a status of 10. consider the below command:


Linux exit command

The above command will close the terminal and return a status of 10. The return statuses are useful as some times they can be tracked to tell error. For example, the return status '0' means the program has successfully executed, and '1' means the program has minor errors.

  • Display the last return status:

To display the exit status of the last executed command, execute the "echo $?" command as follows:

The above command will display the last returned status. Consider the below output:

Linux exit command
  • Exit status of a shell script

Let's create a script, 'Demo.sh.' To create it, execute the following command:

Save the above script by pressing the CTRL+D keys. Now, execute the script by executing the following commands:

The above command will execute the script. Now, check the exit status of the script by executing the below command:

Consider the below output:

Linux exit command

As from the above output, we can see that the exit status of the given script is 0, which means it is successfully executed.

Getting Help

The exit command is a built-in utility. There are no dedicated manual pages available for it. However, it supports the "-help" option, which displays information about the command. To get help, execute the command as follows:

The above command will display help on the command line. We can also execute the help exit command as follows:

Both commands will produce the same output. Consider the below output:

Linux exit command




Latest Courses