# Debugging

Debugging is vital for efficient Bash scripting.

### Debugging Your Scripts

Troubleshoot your Bash scripts by tracing execution. Use `-x` directly or `set -x` for specific sections.

Run your script with `bash -x` to see every command executed:

```bash
bash -x ./your_script.sh
```

For targeted debugging, use `set -x` where you want to start tracing, and `set +x` to stop.

Create `debug_example.sh`:

```bash
touch debug_example.sh
```

Open `debug_example.sh` and add:

```bash
#!/usr/bin/env bash

echo "Starting script."
set -x # Enable debug mode from here
ls -l
my_variable="test_data"
echo "Variable is: $my_variable"
set +x # Disable debug mode
echo "Debugging finished."
```

Save, make executable, and run:

```bash
chmod +x debug_example.sh
./debug_example.sh
```
