What’s Exit (0)
The exit command takes a single argument, which is the standing code to be returned to the shell. A standing code of 0 signifies success. It’s a widespread conference to make use of 0 because the standing code for achievement and this conference is utilized by many applications and scripts and permits different applications and scripts to simply decide whether or not a command or script accomplished efficiently or encountered an error.
# Instance of utilizing exit(0)
echo “This script accomplished efficiently”
exit 0
What’s Exit (1)
A standing code of 1 signifies failure of command and once more it’s a typical observe to make use of 1 if there may be any in any error or failure in command execution, right here is an bash script which makes use of the exit (1):
# Instance of utilizing exit(1)
echo “This script encountered an error”
exit 1
What’s the Distinction between exit(0) and exit(1)
The principle distinction between exit(0) and exit(1) is the standing code returned to the shell. A standing code of 0 signifies that the script or command is executed efficiently with out encountering any type of errors. A standing code of 1 or every other non-zero worth signifies that the script or command encountered an error, right here is instance code that use each exit(0) and exit(1):
# Examine if a file exists
if [ -f “/home/aaliyan/bashfile4.sh” ]; then
echo “File exists”
sleep 5 # Delay for five seconds
exit_status=0 # Set exit standing to success
else
echo “File doesn’t exist”
sleep 5 # Delay for five seconds
exit_status=1 # Set exit standing to error
fi
echo “Exit standing: $exit_status“
exit $exit_status # Exit with the decided exit standing
On this script, if the file exists, the script will print “File exists” and return a standing code of 0 to point success:
If the file doesn’t exist, the script will print “File doesn’t exist” and return a standing code of 1 to point an error:
Conclusion
The exit command in Bash is used to terminate a script or command and return a standing code to the shell. A standing code of 0 signifies success, whereas if the error code is any non-zero digit, then it signifies that an error is encountered. It’s a widespread conference to make use of 0 because the standing code for achievement and any non-zero worth to point an error.