Skip to content

exit Linux command

The Linux exit command is used to gracefully terminate a shell session. It allows users to exit from a terminal window or a script efficiently. By typing ‘exit’ and pressing Enter, the current shell session will be closed, returning the user to the parent shell or logging them out of the system if it’s the last shell running. This command is useful for both interactive and scripted sessions, ensuring that all processes are properly closed before ending the session.

Terminal window
exit [n]
OptionDescription
nThe exit status to return
ParameterDescription
nInteger value representing the exit status (default is 0)
Terminal window
exit 0

Exits the current shell session with a success status code.

Terminal window
exit 1

Exits the current shell session with an error status code.

Terminal window
ls && exit

Executes the “ls” command and then exits the shell session.

Terminal window
echo "Goodbye!" && exit

Prints “Goodbye!” to the terminal before exiting the shell session.

Terminal window
for i in {1..5}; do echo $i; done; exit

Executes a loop printing numbers 1 to 5, then exits the shell session.

Terminal window
sleep 5 && exit

Waits for 5 seconds using the “sleep” command before exiting the current shell session.

To use the exit command in Linux, execute the following command:

Terminal window
exit

How can I exit a shell script with a custom exit status?

Section titled “How can I exit a shell script with a custom exit status?”

To exit a shell script with a custom exit status, use the exit command followed by the desired exit status code. For example:

Terminal window
exit 1

How do I force an immediate exit in Linux terminal?

Section titled “How do I force an immediate exit in Linux terminal?”

To force an immediate exit in the Linux terminal without waiting for processes to finish, use the following command:

Terminal window
exit -f

How can I exit a script if a command fails in Linux?

Section titled “How can I exit a script if a command fails in Linux?”

To exit a script immediately if a command fails, you can use the following syntax in the script:

Terminal window
set -e
command1
command2
exit $?

To exit an SSH session in Linux, use the exit command as follows:

Terminal window
exit

To exit a loop in a bash script, you can use the break command. Here’s an example:

Terminal window
for ((i=1; i<=5; i++))
do
if [ $i -eq 3 ]
then
break
fi
echo $i
done

How can I exit a script with a specific message in Linux?

Section titled “How can I exit a script with a specific message in Linux?”

To exit a script with a specific message in Linux, you can use the following command:

Terminal window
echo "Custom message" && exit

How do I exit a script with an error message in Linux?

Section titled “How do I exit a script with an error message in Linux?”

To exit a script with an error message in Linux, you can use the following command:

Terminal window
echo "Error message" >&2 && exit 1
  • Terminating a shell session
  • Exiting from a script or program
  • Returning an exit status to the parent process