On 2/15/12 7:51 PM, dave reisner wrote: > Hi all, > > I'm noticing some odd behavior in an ERR trap with errtrace. It's > present in bash 3.2 and as well as 4.2. The simplest reproduction is > as follows: > > ------8<---------------------- > #!/bin/bash > > somefunc() { > false > echo "shouldn't see this" > } > > set -E > trap -- 'return 1' ERR > > somefunc > echo "should see this" > ------8<---------------------- > > Both versions of bash throw an error, though fwiw the line number > differs: bash4 blames the line of where the trap is fired versus bash3 > which blames the line where the trap is declared. The entire output of > the script is: > > foo: line 4: return: can only `return' from a function or sourced > script > should see this > > So, both versions give the intended behavior of returning from the > function despite the error. imo, there's a bug here somewhere, I'm > just not sure if it's the faulty error being thrown or if I shouldn't > be expecting the trap to work as it is when it's declared outside of a > function.
You're calling the error trap twice. The first time you call it is after the `false' in the body of the function, and the `return' works as intended there. Since you return 1, the call to `somefunc' fails, triggering the error trap again. The second time you call it, you're not executing in a function context, and `return' throws an error. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRU c...@case.edu http://cnswww.cns.cwru.edu/~chet/