How to Return Exit status “0” in Bash [6 Cases]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The value of exit status “0” indicates the successful completion of a script or command. After executing a function or script, it returns an exit code ranging from 0 to 255 where the non-zero value denotes the failure of the script or function. In Bash scripting, checking the exit code is crucial for handling errors and creating more robust scripts. You can use the return or exit command to return the exit code. In this article, I will explain to you 6 cases demonstrating how to effectively return exit status “0” in Bash.

What is the Purpose of “return 0” in Bash?

The “return 0” is generally used within functions to indicate the successful execution of a function. Every command or function executed in Bash returns an exit code using a return or exit command that signifies whether the executed command or function is successful or not. Usually, the exit code is stored in a variable named $?. The exit code is a numerical value between 0 and 255 where 0 indicates success and a non-zero value indicates failure.

  • return 0: Success.
  • return 1: Generic error.
  • return 2-255: Used for specific error conditions, and the higher the value, the more severe the error.

6 Cases Where Exit Status Returns “0” in Bash

The exit status specifies if a command or script was executed successfully. Both return and exit commands can be used to return exit status. In this section, I will explain 6 cases where the exit status returns 0 meaning successful execution.

1. Successful Execution of a Command

Whether a command is executed successfully or not can be checked with the exit code. When the exit code returns 0, the command execution is successful otherwise it specifies an error. Run the script to check the execution of the ls command:

#!/bin/bash
ls /home/mou/Downloads
echo "Exit status: $?"
EXPLANATION

Here, the ls command prints the contents of the “Downloads” directory. The echo "Exit status: $?" line immediately follows the ls command, so it will correctly print the exit status of the ls command. If the “ls” command successfully lists the contents of the directory, the exit status will be 0. If there’s an error, the exit status will be non-zero, indicating the nature of the error.

successful execution of ls command with return code 0

As you can see, the ls command is executed successfully, so it returns the exit code 0.

2. Successful Execution of a Script

To execute an entire script successfully, the exit command is used. Like the return command, the exit command can also return 0 to 255 numerical values. It will return 0 if the execution of the script is successful. Here’s how:

#!/bin/bash
echo "Hello, World!"
echo "Exit status: $?"  # Prints the exit status
exit 0
EXPLANATION

The echo "Exit status: $?" line will print the exit status of the last command executed, which is the echo "Hello, World!".

successful execution of script

The script returns exit status 0 meaning successful execution of the script.

3. Successful Termination of a Function

A function can be terminated by using a return statement inside the function. If the function returns exit code 0, then the function terminates successfully. Otherwise, the function terminates with errors. Check the bash script to see how the successful termination occurs:

#!/bin/bash
function my_function {
  echo "Welcome to Linuxsimply."
  return 0
}

my_function  # Calls the function
echo "Exit status: $?"  # Prints the exit status of the function
EXPLANATION

In this script, the function my_function is called and it echoes “Welcome to Linuxsimply.” Then, it explicitly returns 0. The function’s exit status can be accessed using $?, which will later be printed with the echo "Exit status: $?" line.

successful termination of function with return code 0

Since the function explicitly returns 0, the exit status is 0, indicating successful termination.

4. Successful Conditional Check

The exit status can return value based on a certain condition. If the specified condition is true, it will return 0. On the other hand, it will return 1. Here’s the entire bash script to get a clear insight:

#!/bin/bash
function conditonal_check {
  if [ 1 -eq 1 ]; then
    echo "The condition is true."
    return 0
  else
    echo "The condition is false."
    return 1
  fi
}

conditonal_check
echo "Exit status: $?"
EXPLANATION

The conditional_check function checks whether the condition “1 equals 1” is true, and based on that, it returns either 0 (true) or 1 (false). After calling the function, it prints the exit status using echo "Exit status: $?".

conditional check using return 0

As the condition is true, it returns the exit status 0.

5. Identifying Errors

Whether a function contains errors can be identified with the return statement. If a function returns 0, then there is no error. But if the function returns 1-255, then there is an error. Here is the bash script to understand how to find the error using the return statement:

#!/bin/bash

function check_network {
  # Simulate checking network connectivity
  if ping -c 1 google.com > /dev/null 2>&1; then
    echo "Network is reachable."
    return 0
  else
    echo "Network is unreachable."
    return 2
  fi
}

# Check network
check_network
if [ $? -ne 0 ]; then
echo "Error: Network check failed with exit status $?."
fi
EXPLANATION

The check_network function simulates checking network connectivity using the ping command to ping Google’s website. If the network is reachable, it returns a success status of 0; otherwise, it returns an error status of 2. Here, $? -ne 0 condition within the if statement checks the exit status of the most recent command. If the exit status is not equal to 0, it prints an error message indicating that the network check failed.

checking network connectivity with return statement

Since the ping command is successful, it prints “Network is reachable.”.

6. Returns “0” from Nested Function

A nested function is a function defined within another function. In a nested function, the return 0 ensures that the entire script signals success if no errors occur throughout the functions. Here’s an example bash script for this:

#!/bin/bash
function inner_function {
  echo "Welcome to Linuxsimply."
  return 0
}

function outer_function {
  echo "Hello, World!"
  inner_function
  return 0
}
outer_function
echo "Exit status: $?"
EXPLANATION

Here, the inner function is defined within the outer function. The outer_function calls inner_function, and both functions use return 0 to indicate successful execution. After calling the outer function, it will print both messages inside the inner and outer function. Finally, the return value is printed by the echo "Exit status: $?".

nested function with return statement

The exit status 0 displayed is that of the outer function.

Practice Tasks on How to Return Exit Status “0” in Bash

Elevate your Bash scripting abilities by practicing the following tasks:

  1. Define a Bash script that returns exit status 0 if a file exits.
  2. Write a function that checks the available disk space and returns 0 if the disk space is greater than 10%.
  3. Create a nested function that returns exit status 1.
  4. Generate a function that takes two strings as arguments. If the strings are equal, it will return exit code 0, otherwise returns 1.

Conclusion

In conclusion, I have explained the basic concept of “return 0” in bash. In this article, I have provided 6 cases of how to return exit status 0 in a bash script or function. Hopefully, this article will help you write better scripts. Happy scripting!

People Also Ask

What is return 0 in bash?

The return 0 in bash is an exit status that indicates the successful execution of a function while the non-zero exit status denotes errors.

Is return the same as return 0?

No, there is a slight difference. The return statement exits the function with an exit code of the last executed command whereas the return 0 exits the function with an exit code 0 (successful execution).

What is return code 1?

The return 1 is the exit status that implies that the command or script encountered an error during execution.

Is exit 0 same as return?

No, they are related but serve different purposes. The exit 0 indicates the successful termination of an entire script, it is not used within functions. In contrast, the return statement is used within a function that does not end the entire script but rather the function.

Related Articles


<< Go Back to Return Values From Bash Function | Bash Functions | Bash Scripting Tutorial

Rate this post
Mitu Akter Mou

Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering & Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio

2 thoughts on “How to Return Exit status “0” in Bash [6 Cases]”

  1. Your shell programming with return 0 is great !
    Pls call me at 01911353885
    I need to know how to make clock with sleep command.

    Reply

Leave a Comment