Chapter 11: Error Handling

11. Chapter 11: Error Handling#

This chapter covers techniques for making Bash scripts reliable and maintainable. You’ll learn how to handle errors gracefully, debug failing scripts, and write defensive code that anticipates problems. Error handling and robust scripting are what separate amateur scripts from production-ready automation.

What You'll Learn

By the end of this chapter, you will be able to:

✓ Handle errors gracefully using exit codes and error checking ✓ Implement try/catch-like patterns in bash ✓ Debug scripts using set -x and other debugging techniques ✓ Write defensive code that anticipates problems ✓ Validate inputs and handle edge cases ✓ Log errors and create audit trails ✓ Recover from failures automatically ✓ Write production-ready, fault-tolerant scripts

Chapter Map

Section

Topic

Key Concepts

1102

Error Codes & Exit Status

$?, testing for success, trap

1103

Error Handling Patterns

Error checking, recovery, cleanup

1104

Defensive Scripting

Input validation, boundary checking

1105

Debugging Techniques

set -x, set -e, error tracing

1106

Lab: Robust Script

Building production-quality code

Why This Matters

Error handling separates amateur scripts from production systems:

  • Reliability: Detect problems and recover automatically

  • Debugging: Quickly identify where things went wrong

  • User experience: Clear error messages instead of cryptic failures

  • Automation: Unattended scripts need robust error handling

  • Compliance: Audit trails and logging for security

Real-world example: A deployment script without error handling might partially deploy, leaving the system broken. With proper error handling, it detects problems and rolls back.

Prerequisites

You should already understand:

  • Bash scripting basics (Chapters 5-8)

  • Control flow and conditionals (Chapters 6-7)

  • Function definitions (Chapter 8)

  • Basic I/O (Chapter 9)

What You'll Learn

By the end of this chapter, you will be able to:

✓ Handle errors gracefully using exit codes and error checking ✓ Implement try/catch-like patterns in bash ✓ Debug scripts using set -x and other debugging techniques ✓ Write defensive code that anticipates problems ✓ Validate inputs and handle edge cases ✓ Log errors and create audit trails ✓ Recover from failures automatically ✓ Write production-ready, fault-tolerant scripts

Chapter Map

Section

Topic

Key Concepts

1102

Error Codes & Exit Status

$?, testing for success, trap

1103

Error Handling Patterns

Error checking, recovery, cleanup

1104

Defensive Scripting

Input validation, boundary checking

1105

Debugging Techniques

set -x, set -e, error tracing

1106

Lab: Robust Script

Building production-quality code

Why This Matters

Error handling separates amateur scripts from production systems:

  • Reliability: Detect problems and recover automatically

  • Debugging: Quickly identify where things went wrong

  • User experience: Clear error messages instead of cryptic failures

  • Automation: Unattended scripts need robust error handling

  • Compliance: Audit trails and logging for security

Real-world example: A deployment script without error handling might partially deploy, leaving the system broken. With proper error handling, it detects problems and rolls back.

Prerequisites

You should already understand:

  • Bash scripting basics (Chapters 5-8)

  • Control flow and conditionals (Chapters 6-7)

  • Function definitions (Chapter 8)

  • Basic I/O (Chapter 9)