The “exit” Command in Linux [5 Practical Examples]

There are multiple ways to exit or log out from the current shell or job in the Linux environment. When we take a leave from shell windows, it may affect the backend jobs. So, we need a proper way so that there will be no impact even though the user or shell window will exit from the Linux environment. You can exit the shell where it is running by using the exit command in Linux. In this article, I will describe all the features and uses of the exit command in Linux with multiple examples.

A. Description

The exit command is used to end or close the current login session in the Linux ecosystem. Shell or bash jobs as well as the Linux CLI can be closed using this simple command.

B. Syntax

The syntax of the exit command in Linux  is very simple which is as below:

exit [INTEGER_VALUE] USER_NAME

Note: You can provide the integer value and which can be used for further system calls. Again, there are no OPTIONS available for the exit command in Linux.

Practical Examples of the “exit” Command in Linux

The exit is a helpful command which helps to take a safe exit from any shell or job or bash jobs. In the section below I will show you some useful applications with exit commands in Linux.

Example 1: Exiting from Current Shell Using the “exit” Command

You can take an exit from the current shell by simply typing exit. Here, I will take an exit from the current shell by using the exit command. To do the same, follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

 Type the following command in the command prompt:

exit

➌ Now, press the ENTER button.

Output >

Before executing the exit command, you can see the terminal.Terminal is operating before executing the exit command in linux.After executing the exit command, the terminal has closed.The terminal has closed after executing the exit command in linux.

Example 2: Exiting from the Root User’s Shell Using the “exit” Command

You can take an exit from the root user by simply typing exit. Here, I will take an exit from the root user’s shell by using the exit command. To do the same follow the steps below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

 Type the following command in the command prompt:

sudo su

➌ Now, press the ENTER button.

By typing your password, you will log in as root_user.

➍ Then, type the following command in the command prompt:

exit

➎ Now, press the ENTER button.

Output >

In the image below, you can see that the root user takes an exit by leaving a message exit” in the terminal.The process of exiting the root user with the exit command

Example 3: Exit Command With Default Value

In the Linux environment, every job or command has a default exit code. The default successful value exit code is “0” for each command. If you get any other value except “0” then there will be some issue in the job or command.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

echo “hello world”

➌ Now, press the ENTER button.

You will get the output “ hello world“ in the terminal.

➍ Then type the following command in the command prompt:

echo $?

➎ Again, press the ENTER button.

You will get ‘0’ which means there is no issue in the command.

➏ Now, Type the following command in the command prompt:

echo “hello world

➐ Press the ENTER button again.

You will get a ‘>’ sign which is due to any unexpected error.

➑ press CTRL+C and then type the following command in the command prompt:

echo $?

After executing the echo $? command, you will not get ‘0’ as before, which means there is an issue.

➒ Now, press the ENTER button.

Output >

When the echo “hello world” command is executed successfully there you get ‘0’ as the exit code but after executing a faulty command the exit code becomes non-zero.The default value of the exit command in linux.



Example 4: Exit Command With Manual Value

You can set a manual exit code for a job. To do so, you have to prepare a shell job that can be written with the nano command. In the section below I will show you, how to set a manual value as an exit code. Here I set the exit code 60.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

nano code.sh

Open nano terminal.

➌ Now, press the ENTER button.

➍ type the following text in the nano text editor.

#!/usr/bin/bash
echo "Hello world"
exit 60

Bash script for exit command in linux with manual value.

➎Put the following command into action.

bash ./code.sh

➏ Now, press the ENTER button.

You will get the output as “Hello world”

➐Then, execute the command below.

echo $?

Output >

After executing the echo $? command, you will get the manual exit code on the terminal shown below.Exit Command With Manual Value

Example 5: Exit Command Help

You can get help about the exit command by using the help command. The process is very simple and you can get help by the process described below.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

help exit

➌ Now, press the ENTER button.

Output >

You can see the help page on the terminal and can get the required help with the exit command.Help page of the exit command.

Conclusion

I’ve demonstrated the operation of the exit command in this article. Additionally, I have included some helpful examples. If you carefully read this article, you will be able to use the exit command effectively on your own.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
S. M. Amdadul Islam

Hello everyone. I am S. M. Amdadul Islam, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Naval Architecture and Marine Engineering (NAME) graduate from the Bangladesh University of Engineering and Technology. In addition to my regular work, I enjoy watching Youtube, discovering new things, gossiping with friends, visiting new places, and taking photos of landscapes. Read Full Bio

Leave a Comment