Updated April 14, 2023
Introduction to Linux Exit
In the Linux ecosystem, the exit command is used to exit or close the current login session. It can be used in the Linux CLI as well as in the shell or bash jobs. In Linux, the exit command comes under the util-linux package.
Syntax of Exit Command
exit [ integervalue ] exit user name
- exit: We can use the exit keyword in the syntax or command. It will accept one of the arguments like numeric value or the user name. The exit command will help to exit the current shell prompt or exit the job or application.
- integer value: We can provide the numeric value in the exit command. The same numeric value will use for the further system call
- user name: Inexit command; we can provide the user name. The pass user name will exit the current login in session from the environment.
How does Linux Exit Command work?
In the Linux environment, we have multiple ways to exit or log out from the current shell or bash job or any Linux application. The exit utility comes under the util-Linux package. When we are exiting from the shell windows, the backend jobs will also be affected. Due to which we need to make a proper plan for job execution or define the backend generic process to run the jobs in backed. It will no impact even though the user or shell window will exit from the Linux environment.
The exit command is majorly used in the shell or bash jobs. We can define the different exit values in the shell jobs. The same exit values we can call or use in the shell windows to check the shell job status. There is no barrier for the numeric or integer values. We can use any integer value with the exit command.
Examples to understand Linux Exit Command
Different examples are mentioned below:
Example #1 – Exit Command
In the exit command, we are able to exit or logout from the current login shell or the Linux environment. Once the user will log out with the help of the exit command, then the scope of the user will also go down (it is special when the multiple users are login in the same SSH/Terminal session)
The exit command is wieldy used to logout from the Linux profile. Currently, we have login with the root user and switch from root user to test user (refer to screenshot 1 (a)). Once the user is able to switch from root to test user profile, the roles and privileges will also be changed. When we are executing the “exit” command. The user will logout from the test user with the notification message “logout” (refer to screenshot 1 (b)).
su - test
Screenshot 1 (a)
Screenshot 1 (b)
Example #2 – Exit Command: With Default Value
In the Linux environment, every job or command is having the exit code. Thus, with the help of the same exit code, we can identify the job or command execution status.
echo "Hello, EDUCBA" echo "Hello, EDUCBA echo $?
In the Linux ecosystem, the command or the job has a specific exit code. If the exit code comes except “0” zero, then there is an issue in the job or command. We have written the echo command and try to fetch the exit code. We have got the “0” exit code, i.e. the command is executed successfully (refer to screenshot 2 (a)). In this instance, we have kept the double quotes and execute the command. However, we have got a different exit code, not the “0”. Hence we have concluded that there is an issue in the command (refer to screenshot 2 (b)).
Screenshot 2 (a)
Screenshot 2 (b)
Example #3 – Exit Command: With Manual Value
In the exit command, we can set the manual exit code at the end of the shell job. Then, we can tress the job execution status with the same manual exit code.
#!/usr/bin/bash echo "Hello" exit 6
As per the above shell job, we have simply returned the shell job for echo command. In this, we have entered the echo command and exit value (6). Therefore, when we are executing the above shell job, the exit code should be returned 6. If exit code 6 will not write on the shell windows, we can conclude that there is an issue in the job or the application level.
As per the screenshot 3 (a) below, we are in the data directory and triggering the shell job, i.e. “test_job.sh”. Once the job will trigger, we can fetch the exit code via echo command. We can use the Linux system variable “$” and call the “$” value with the help of the echo command. For example, if the “$” value would be “6”, then only the shell job will be executed fine (refer to screenshot 3 (b)).
Screenshot 3 (a)
Screenshot 3 (b)
Example #4 – Exit Command: Help
We are having the functionality to get help with the exit command. For getting any help, we need to use the “help” keyword before the exit command.
As per the above command, we are able to get basic help with the exit command. It will print the general information like how the command is, what does command does, etc.
We have seen the uncut concept of “Linux Exit Command” with the proper example, explanation and command with different outputs. The exit command is widely used to exit the shell windows or logout form the current profile. Every command or job is having the exit code.
We hope that this EDUCBA information on “Linux Exit” was beneficial to you. You can view EDUCBA’s recommended articles for more information.