Bash:
Printing debug output

How to:

#!/bin/bash

# Define a variable
name="Gizmo"

# Print variable for debugging
echo "Debug: The variable name is $name"

# Conditional with debug output
if [[ $name == "Gizmo" ]]; then
    echo "Debug: Entered the if-statement."
    # Do something
fi

# Loop with debug output
for i in {1..3}; do
    echo "Debug: Loop iteration $i"
    # Do something in loop
done

Output:

Debug: The variable name is Gizmo
Debug: Entered the if-statement.
Debug: Loop iteration 1
Debug: Loop iteration 2
Debug: Loop iteration 3

Deep Dive

Originally, debugging meant removing physical bugs that disrupted early computers. Nowadays, it’s about squashing code bugs. Debug outputs are the programmer’s magnifying glass.

Alternatives to echo in bash scripts include printf for more formatting options or writing to a file with redirection > for persistent logs.

Bash also supports conditional debug output with the built-in set -x to trace commands and their arguments as they’re executed. set -x is great for full-script debugging.

See Also