FUNDAMENTALS A Complete Guide for Beginners
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: $?"
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.
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
The echo "Exit status: $?"
line will print the exit status of the last command executed, which is the echo "Hello, World!"
.
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
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.
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: $?"
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: $?"
.
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
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.
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: $?"
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: $?"
.
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:
- Define a Bash script that returns exit status 0 if a file exits.
- Write a function that checks the available disk space and returns 0 if the disk space is greater than 10%.
- Create a nested function that returns exit status 1.
- 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
- How to Exit From Function in Bash? [5 Ways]
- How to Return String From Bash Function [4 Methods]
- How to Return Boolean From Bash Function [6 Methods]
- How to Return Array From Bash Function? [7 Methods]
<< Go Back to Return Values From Bash Function | Bash Functions | Bash Scripting Tutorial
Your shell programming with return 0 is great !
Pls call me at 01911353885
I need to know how to make clock with sleep command.
Thanks for your feedback. To know how to make a clock with the “sleep” command, go through this article https://linuxsimply.com/bash-scripting-tutorial/loop/while-loop/sleep/#4_Creating_a_Simple_Clock_Using_sleep_Command. Feel free to knock me if you have any confusion regarding this.