bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Behavior of calling return from a trap


From: Chet Ramey
Subject: Re: Behavior of calling return from a trap
Date: Tue, 10 Apr 2012 12:04:36 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2

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    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]