Behavior of calling return from a trap
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. Regards, Dave
Re: Behavior of calling return from a trap
On Tue, Apr 10, 2012 at 12:04:36PM -0400, Chet Ramey wrote: > 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. Ah, of course. That makes a lot of sense. Thanks for clarifying. dave