πŸ”§ 4. Bash Functions & Scripting
Estimated reading: 3 minutes 378 views

Bash: Debugging – Use set -x, -n, and -v to Troubleshoot Scripts


Introduction to Bash Debugging – Master set -x, set -n, and set -v

Debugging is a crucial part of writing reliable Bash scripts. Bash provides built-in options like set -x, set -n, and set -v to help you trace script execution, detect syntax errors, and understand how your script behaves in real-time.

These debugging flags are simple yet powerful tools to make your script development faster, more predictable, and less error-prone.


In this article, you’ll learn:

  • What set -x, -n, and -v do in Bash
  • How to enable and disable these debugging modes
  • Example outputs to see how each works
  • Best practices for script debugging

Debugging Flags Overview

FlagPurpose
-xPrint each command as it’s executed (trace)
-nParse-only mode (check syntax, don’t run)
-vPrint shell input lines as they’re read

You can enable them using either:

set -x     # or
set -n     # or
set -v

Or combine them:

set -xv

To turn them off:

set +x     # disables tracing

🐾 set -x – Trace Command Execution

Shows each command and its arguments as they execute. Great for spotting logic errors.

Example:

#!/bin/bash
set -x

name="Vaibhav"
echo "Hello, $name"

Output:

+ name=Vaibhav
+ echo 'Hello, Vaibhav'
Hello, Vaibhav

Helpful for checking variable expansion and command flow.


set -n – Check Syntax Without Running

Checks for syntax errors without executing any commands.

Example:

#!/bin/bash
set -n

echo "Start script"
echo "Missing quote
echo "Script continues"

Output:

myscript.sh: line 4: unexpected EOF while looking for matching `"'

Commands are not executedβ€”this is a safe dry-run to detect typos.


set -v – Echo Input Lines Before Execution

Displays each line as it is read, before it’s executed. Useful for reviewing script input.

Example:

#!/bin/bash
set -v

echo "First"
echo "Second"

Output:

echo "First"
First
echo "Second"
Second

Helps understand what code the shell is reading, especially with complex logic.


Combine Debugging Options

Use combinations for deeper insights:

set -xv   # Trace execution and input lines

Disable Debugging Mid-Script

set -x
echo "Tracing ON"
set +x
echo "Tracing OFF"

Output:

+ echo 'Tracing ON'
Tracing ON
Tracing OFF

This approach is great for isolated debugging.


Best Practices for Debugging

TipWhy It Matters
Use set -n to validate syntaxAvoid running broken logic
Isolate blocks with set -x/+xFocus debug output on suspicious code
Don’t leave debug flags in prodKeeps scripts clean and silent
Use comments for toggle zonesEasier collaboration and reuse

Summary – Bash Debugging Options

Bash debugging options like set -x, set -n, and set -v are essential for writing reliable and bug-free scripts. Use them wisely to trace execution, check syntax, and view inputs during development.

Key Takeaways:

  • set -x shows command execution with expanded variables
  • set -n performs a syntax-only dry run
  • set -v displays each input line before execution
  • Combine or isolate debug sections for targeted analysis

Real-world Uses:

  • Troubleshooting conditional logic or loops
  • Verifying cron job behavior without execution
  • Understanding input flow in complex scripts

FAQ – Bash Debugging Options


Can I combine multiple set flags?
Yes, like set -xv to trace both input and execution.


Does set -n execute the script?
No. It parses the script and checks for syntax errors without running anything.


How do I stop debugging mid-script?
Use set +x or set +v to disable the corresponding mode.


Is set -v useful without set -x?
Yes. It shows what’s being read line-by-line, which helps for flow review.


Should I keep set -x in production scripts?
No. It clutters output and may expose sensitive values. Use only for debugging during development.


Share Now :
Share

🟒 Bash: Debugging (set -x, -n, -v)

Or Copy Link

CONTENTS
Scroll to Top