Files
linux.softwareshinobi.com/landing/docs/Bash-Scripts/0131-debugging.md

45 lines
1007 B
Markdown
Raw Normal View History

2025-06-19 10:03:08 -04:00
# Debugging
Ensure your Bash scripts run flawlessly. Master debugging to identify issues quickly and build robust code.
### 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
```
Master these debugging techniques. They are indispensable for building high-quality, dependable Bash scripts.