USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Error Handling and Debugging

Lesson 7/40 | Study Time: 20 Min

Effective error handling and debugging are critical skills for producing reliable and maintainable Linux shell scripts. Shell scripts often run unattended in production environments where unexpected errors can disrupt automation workflows or system stability.

By understanding exit codes, using trap handlers, employing error checking strategies, and leveraging debugging tools and shell options, scriptwriters can identify problems quickly, handle failures gracefully, and maintain robust scripts over time. 

Exit Codes

Every Linux command returns an exit status as an integer from 0 to 255 upon completion.


1. 0 indicates success.

2. Non-zero values indicate various error conditions.

You can access the exit code of the last executed command through the special variable $?.


Example:

bash
mkdir /tmp/mydir
if [ $? -ne 0 ]; then
echo "Failed to create directory"
else
echo "Directory created successfully"
fi


Checking exit codes enables scripts to make decisions based on the success or failure of commands.

Trap Handlers

The trap command registers handlers for specific signals or script events, allowing cleanup or custom error processing before the script exits or continues.


Common traps:

ERR: Executes on any command error (non-zero exit).

EXIT: Executes on script exit regardless of success or failure.


Example: 

bash
trap 'echo "Error occurred. Cleaning up..."; rm -rf /tmp/tempfile' ERR
touch /tmp/tempfile
mkdir /root/protected_dir # Likely to fail, triggers trap


Traps help maintain system integrity by ensuring resources are cleaned and errors logged.

Error Checking Strategies

Error checking is a critical part of writing reliable and safe shell scripts. The strategies listed below demonstrate practical ways to handle failures and log errors.


1. Use if or case blocks to check exit codes explicitly after critical commands.

2. Combine commands with && (and) and || (or) for inline handling:

bash
mkdir /tmp/mydir && echo "Success" || echo "Failed"


3. Redirect error messages to logs using 2>> for later analysis.

Script Debugging Techniques

These debugging techniques help make scripts more transparent and easier to troubleshoot. The list below covers commonly used options and tracing methods in Bash.


1. Enable debugging mode with set -x to print each command and its arguments as they execute.

2. Use set -v to print shell input lines as read.

3. Combine debugging and safety options:

bash
set -euxo pipefail


4. -e: Exit on any command failure.

5. -u: Treat unset variables as errors.

6. -x: Print commands as executed.

7. -o pipefail: Return failure if any command in a pipeline fails.

8. Insert manual echo or logger statements to trace script flow.

Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.