Trapping errors in Bash
2013/08/19
I want to begin with a warning. The bash shell is really quite powerful and it is possible to do some amazingly difficult scripting using nothing more.
If you have any doubt of that, reading Chris F. A. Johnson's Shell Scripting Recipes: A Problem-Solution Approach would convince you. If Chris can write crossword puzzles using shell scripts, there's probably not much that can't be done.
That doesn't mean that you should. Complex shell scripts can be very hard to understand, impossible to modify by anyone but a true expert and are not necessarily transportable. Those statements can be true for any computer language, but shell scripts often cannot be written clearly when they reach a certain level of complexity.
The concepts demonstrated here don't reach that level, but I suspect that you'll see that they easily could. If you want to dive in and push these ideas to the limit, you can certainly do so. Just don't ask me to debug anything like that!
With that in mind, let's trap some errors.
A simple trap
This is as simple as it gets:
#!/bin/bash trap 'echo Sorry, there was an error at line $LINENO' ERR ls /etc/no_such_place_or_file 2>/dev/null echo "foo" ls /etc/no_such_place_or_file 2>/dev/null
Upon invocation that will spit out:
Sorry, there was an error at line 3 foo Sorry, there was an error at line 5
(Unless you have a file or directory named "/etc/no_such_place_or_file", of course)
That's helpful. There is a slight problem, though:
#!/bin/bash trap 'echo Sorry, there was an error at line $LINENO' ERR for x in 1 2 3 4 5 do ls /$x/nosuchplace 2>/dev/null ls /$x/someotherplace 2>/dev/null done
That always says the line number is the top of the loop
Sorry, there was an error at line 3 Sorry, there was an error at line 3 Sorry, there was an error at line 3 ...
That's because any control structure (if, while, for, until, case) is seen as one multi-line command. There's no point to that example other than to show that.
We can do something like:
trap 'echo Sorry, there was an error at line $LINENO with $x' ERR
and get:
Sorry, there was an error at line 3 with 1 Sorry, there was an error at line 3 with 1 Sorry, there was an error at line 3 with 2 Sorry, there was an error at line 3 with 2 ..
Which is some help, but control structures will obfuscate your errors.
We can get far more complicated:
#!/bin/bash function my_trap() { [ -lt 15 ] && echo bad bad line error with x at $x [ -ge 15 ] && echo REALLY bad at line error } trap 'my_trap ${LINENO} $? ' ERR for x in 1 2 3 4 5 do ls /$x/nosuchplace 2> /dev/null done echo foo ls /100/foo 2>/dev/null
Which should produce:
`bad bad line 11 error 1 with x at 1 bad bad line 11 error 1 with x at 2 bad bad line 11 error 1 with x at 3 bad bad line 11 error 1 with x at 4 bad bad line 11 error 1 with x at 5 foo REALLY bad at line 17 error 1
Or, you can reset the trap:
#!/bin/bash function my_trap() { [ -lt 15 ] && echo bad bad line error with x at $x [ -ge 15 ] && echo REALLY bad at line error } trap 'my_trap ${LINENO} $? ' ERR for x in 1 2 3 4 5 do ls /$x/nosuchplace 2> /dev/null done echo foo trap 'echo Sorry, there was a somewhat important error at line $LINENO ' ERR ls /100/foo 2>/dev/null
Which yields:
bad bad line 11 error 1 with x at 1 bad bad line 11 error 1 with x at 2 bad bad line 11 error 1 with x at 3 bad bad line 11 error 1 with x at 4 bad bad line 11 error 1 with x at 5 foo Sorry, there was a somewhat important error at line 17
What if you would rather bail out at the very first error?
You could add a "set -e" at the top of your script. That stops on any error, though, so a better way might be to add an "exit" in your trap function that is seen when and where you want it to be seen.
Finally, traps aren't just for errors.
trap "echo All Done" 0 trap OnExit EXIT INT TERM
The bash man page has extensive (and quite complex) information about "trap" and all its gotchas.
Got something to add? Send me email.
Increase ad revenue 50-250% with Ezoic

Inexpensive and informative Apple related e-books:
Take Control of Upgrading to Yosemite
iOS 8: A Take Control Crash Course
Yosemite Crash Course
Are Your Bits Flipped?
Sierra: A Take Control Crash Course
More Articles by Anthony Lawrence
Find me on Google+
© 2013-08-19 Anthony Lawrence
Printer Friendly Version
Trapping errors in Bash Copyright © August 2013 Tony Lawrence
Have you tried Searching this site?
Support RatesThis is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.
Contact us
Printer Friendly Version