Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

set -e in Bash scripts: discovering failure location?

As we all know, we should use set -e to make Bash scripts more robust. However, with this enabled, if the script does fail, is there any way to get Bash to report which line of the script failed? At the moment, I'm having to re-run the script with a load of echo commands.

Upvotes: 3

Views: 479

Answers (3)

Matthew Slattery
Matthew Slattery

Reputation: 46998

The best option I'm aware of for debugging with line numbers is to customise the "+" prefix printed by set -x to include $LINENO.

e.g. at the top of your script:

PS4='($LINENO)+ '
set -x

(Not exactly what you asked for, but it is handy!)

Upvotes: 5

ata
ata

Reputation: 2065

Not sure about line numbers, but if you combine -e (or -u) with -v you'll get the line where the error does occur as the last line printed in screen.

Upvotes: 0

Martijn
Martijn

Reputation: 1620

You could just re-run with bash -x (or put set -x in your script to activate debugging, set +x to deactivate it later)

Upvotes: 2

Related Questions