Bash debugging

Introduction

Once in a while i need to debug a bash script that i wrote, but i need to search how to get the line number of debug to a file, so easier for me to put it in mij blog, maybe it will help somebody else also.

Howto

Enable debugging

set -x

Add line numbers

PS4='$LINENO: '

Debug to external file

exec 5> debug_output.txt
BASH_XTRACEFD="5"
PS4='$LINENO: '

set -x

Disable debug

set +x

Logging to syslog

#!/bin/bash

exec 5> >(logger -t $0)
BASH_XTRACEFD="5"
PS4='$LINENO: '
set -x

# Place your code here

Richard

Previous Post Next Post