Exit codes are integer numbers from 0-255 used to indicate whether a script has run successfully or not. Effective process handling such as debugging, and error management using Bash exit codes make it vital to learn how Bash sets exit codes. This guide will discuss how to set Bash exit code effectively. Additionally, it will show how to set customized exit codes in Bash scripts and how to exit Bash functions using these codes mentioning shell scripting examples.
Set Exit Codes in Bash Scripts
To set an exit code in Bash, use the exit command and mention the code number (the integer value from 0-255) afterward. For instance, exit 0 denotes success while exit 1 denotes a specific failure. Thus, the complete syntax is:
exit [N]
Here, the Bash script terminates its execution from where the exit command is called and returns the specified exit code N to the calling process denoting its successful execution or any encountered error condition.
Below is an example script to show how to set exit codes in Bash for effective handling of processes and script termination:
#!/bin/bash
# Read a number from the user
read -p "Enter a number: " number
# Check if the number equals 10
if [ "$number" -eq 10 ]; then
echo "Number found."
exit 0 # Exit with code 0 if number equals 10
else
echo "Number not found."
exit 1 # Exit with code 1 if number does not equal 10
echo "the script ends"
Here, the Bash script above takes a number as input from the user employing the read command and checks if the number equals 10. If yes, the script proceeds with the if block terminates with code 0 denoting success. Otherwise, the else block is executed and the script exits with the set code 1 denoting the error condition. Due to an exit code being set, the last line will not be executed in both cases.
Note: If you use the exit command without any code number, the script will exit its execution with the exit code of the last executed command within the script to denote success or failure.
How to Set Custom Exit Codes in Bash?
Apart from using exit codes by preserving their usual meanings, you can also use custom exit codes with the exit command within scripts. This allows you to terminate scripts using your own set of status codes while indicating if the execution was successful or if it encountered an error.
Look at the Bash script below to understand how to set custom codes on exit in Bash:
#!/bin/bash
# Check if a file exists
if [ -f "myfile.txt" ]; then
echo "File exists."
exit 40 # exits with custom code 40 denoting success
else
echo "File not found."
exit 101 # indicating failure
fi
The above Bash script checks if a file named “myfile.txt” exists in the current directory. If the file exists, it prints “File exists.” and exits with a custom exit code 40 denoting success. If the file doesn’t exist, it prints “File not found.” and exits with exit code 101 indicating failure. Thus, the script demonstrates how to set custom exit codes for success and failure scenarios using the if-else conditional checking.
Conversely, the below image shows the successful exit of the same script after finding the file hello.js with the custom code 40 instead of the standard status code 0 for success:
Set Bash Function Exit Code Using “return” Command
In Bash, it is possible to set the exit codes of a function using the return command with the syntax return [N]
. It allows the Bash function to communicate its success or failure to the calling process using the exit status. For instance, return 0 denotes the success of the function’s execution. However, it’s important to note that the return command only exits from the function, not the entire script. Here’s a practical example:
#!/bin/bash
# Define a Bash function with a custom exit code
my_function() {
if [ "$1" -eq 0 ]; then
echo "Success"
return 0 # Exit with code 0 for success
else
echo "Failure"
return 101 # Exit with code 101 for failure
fi
}
# Call the function with different arguments
my_function 0 # Calling with argument 0
echo "Exit code: $?"
my_function 1 # Calling with argument 1
echo "Exit code: $?"
echo "Exit code: $?"
where the special variable, $?, holds the code. Conclusion
Setting up proper exit codes while working with Bash scripts provides users with effective ways to handle script execution including smooth error handling and debugging. This tutorial discusses how to set exit codes in Bash using the exit command. In addition, it has mentioned the return command to set exit codes inside a Bash function and shed light on setting up custom exit codes in Bash.
People Also Ask
How do I get exit codes in Bash?
To get exit codes in Bash, you can use the special variable $? after executing a command or script. This holds the exit code of the most recently executed command denoting its success or failure. For instance, after running the pwd
command in the terminal, you can run the command echo $?
to display the exit code of the pwd command (would be 0 after a successful run or any non-zero value to indicate an error).
What is a Bash exit code?
An exit code in Bash is a numeric value returned by a command or script to indicate the outcome of executing the command or script, with conventionally 0 indicating success and non-zero values indicating various types of error conditions or failures. Exit codes are commonly used for error handling, automation, and programmatic decision-making in shell scripts.
How can I set a specific exit code in a Bash script?
To set a specific exit code in a Bash script, you can use the exit command followed by the desired numeric value to indicate the success or any error condition employing the syntax exit [N]. For example, you can use the exit 42
command to indicate the success of a Bash script instead of 0 which is the usual code for success. As a result, the script will exit with code 42 after its successful execution.
Can I use exit codes inside a function in Bash?
Absolutely. Just define the Bash function, write the set of commands you want to execute inside the function, and mention exit codes in the usual way using the exit command. This will provide a way to convey the success or error condition of the function to the calling process. For instance, in a function to find a file with a particular pattern, exit status 0 can denote a successful search of the file while exit code 1 can notify failure. However, using the return command is the preferred approach to set and return the exit code of a function.
Can I use the exit command without specifying any status code in Bash?
Yes, you can use the exit command without specifying any exit status code that the command or script returns to indicate success or failure. Without this, the exit command returns the code of the last executed command or process. This allows an efficient way to terminate a script, for example, without explicitly defining exit codes.
What do exit codes do in a Bash script?
Exit codes in a Bash script indicate the success or failure status of a script’s execution. When a script finishes running, it returns the exit code to the shell, signaling if it has executed successfully (with exit status zero) or encountered an error (with a non-zero exit code). This allows smooth error handling. Moreover, it enables other processes to react accordingly based on the exit code of the executed script.
Related Articles
- 4 Methods to Exit Bash Scripts on Error with Message
- Usage of “exit” Command in Bash [Explained]
- How to Set & Use Pipefail in Bash [Explained Guide]
- Bash PIPESTATUS to Handle Exit Codes of Piped Commands
- [4 Methods] How to Check Exit Code in Bash?
- [Explained] What Are “exit 0” and “exit 1” in Bash?
<< Go Back to Exit Codes in Bash | Bash Process and Signal Handling | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners