Linux Commands

Differences between the return and exit Commands

The purpose of the return and exit commands are different from each other in Linux. The purpose of the exit command is to terminate the running bash script or terminal and is the last command executed in the script. The function of the return command is to exit the bash function. The returns exit with the return value of the function. In this tutorial, we will discuss in detail the difference between the return and exit commands in Linux.

exit Command in the Linux

In the Linux terminal, the return and exit commands are used to exit from the terminal and script. The exit in Linux is used for terminating the terminal session. When you run the below-mentioned command the terminal will exit and all the running processes will also terminate.

exit

The exit command is the built-in utility of Linux, to get help execute the below command:

exit --help

return Command in the Linux Terminal

The return command is used in the script to return the value called in the function. The return command is always used in the function, if used outside the function it has no effect. This command stops the execution of the function where it is used.

In the below return example first I have created the function subtract, for subtracting the two values.

Return is the built-in utility, get the help via the below command:

return --help

How to Use return Command in Linux Bash Script

The function of return in the bash script is the same as in the terminal. The return command in the bash script is used within the function and returns the value of the function where it is inserted. Let’s take the example of the following bash script with the return value:

#!/bin/bash

# Example function that returns a value
function example_function {
  echo "This is an example function."
  num1=20
  num2=22
  sum=$(( $num1 + $num2 ))
  return $sum
}

# Call the example function
example_function

# Capture the exit status of the example function
example_function_return_value=$?

# Print the return value of the example function
echo "The example function returned:" $example_function_return_value

The $? is the special variable that holds the return value and the last executed command. In the above example, the last executed command is the return 42 statement within the example function.

How to Use exit Command in Linux Bash Script

The exit command is used to terminate or exit from the bash script. It does not matter where we used the exit command in the bash script. The exit function takes the number as an argument and returns the value. If we close the script with the exit command with some parameter it will return a status.

In the example written below, we have used the exit with the value 1 within the exit function. 1 means the program has successfully executed without errors. We have added the echo in the script to immediately exit the script after executing the exit command. The sleep 5 will display the line This is an exit function for 5 seconds before terminating the session:

#!/bin/bash

# Example function that exits the script
function exit_function {
  echo "This is an exit function."
  sleep 5
  exit 1
}

# Call the exit function
exit_function

# This line will never be reached, because the script was terminated by the exit function
echo "This line will never be printed."

Key Difference Between Return and Exit Command in Bash

The following are the key differences between the return and exit commands in Linux:

Return Exit
It is used to return a value of a function in bash scripting It is used to exit the session of the terminal or bash script
The return command is always used within the function The exit can be used anywhere in the terminal

Conclusion

The return and exit are two different commands in Linux, which we have discussed in detail with the examples. The exit command is used to exit from the script or terminal; it can be used anywhere in the script. The return command is used to stop the execution of the function and return the value of the specific function.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.