# # Trapping errors in Bash
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Trapping errors in Bash

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© August 2013 Anthony Lawrence

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()
{
[ $1 -lt 15 ] && echo bad bad line $1 error $2 with x at $x
[ $1 -ge 15 ] && echo REALLY bad at line $1 error $2
}

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()
{
[ $1 -lt 15 ] && echo bad bad line $1 error $2 with x at $x
[ $1 -ge 15 ] && echo REALLY bad at line $1 error $2
}


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.


If you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> Trapping errors in Bash


Inexpensive and informative Apple related e-books:

Photos: A Take Control Crash Course

Take control of Apple TV, Second Edition

Take Control of iCloud, Fifth Edition

Take Control of Upgrading to El Capitan

Are Your Bits Flipped?





More Articles by © Anthony Lawrence





Printer Friendly Version

Have you tried Searching this site?

This 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





There is no programming language, no matter how structured, that will prevent programmers from making bad programs. (Larry Flon)




Linux posts

Troubleshooting posts


This post tagged:

Basics

Linux

Unix



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode