Last Updated: February 25, 2016
·
3.454K
· trinitronx

Debug Bash Scripts with Line Numbers & more info

Need more info to trace your bash script execution?

No problem, just use the PS4 variable:

export PS4='(${BASH_SOURCE}:${LINENO}): - [${SHLVL},${BASH_SUBSHELL},$?] $ '

Then just run bash with trace mode on with one of:

bash -x my_script.sh

or add the flag to the shebang line in your script:

#!/bin/bash -x
echo "hello world"

or use set -x to start trace output, set +x to stop:

#!/bin/bash
echo 'hello world'
echo 'do some things'
set -x
some_command_i_want_to_debug
set +x
echo 'do more things without trace output'

2 Responses
Add your response

or
set -x on the place in the script where you want to start debug and set +x when you want to stop

over 1 year ago ·

@kevit: Thanks for reminding me to put that part in (was short on time when I created the tip, so I forgot)

over 1 year ago ·