bug-bash
[Top][All Lists]
Advanced

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

Re: trap 'echo "trap exit on ${LINENO}"' EXIT -> wrong linenumber


From: Chet Ramey
Subject: Re: trap 'echo "trap exit on ${LINENO}"' EXIT -> wrong linenumber
Date: Fri, 17 Sep 2010 20:37:31 -0400
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.8) Gecko/20100802 Lightning/1.0b2 Thunderbird/3.1.2

On 9/14/10 3:44 AM, Jochen Dekeyser wrote:
> Bash Version: 4.1
> Patch Level: 7
> Release Status: release
> 
> Description:
>         Run this script:
> 
>         #!/bin/bash
> 
>         trap 'echo "trap error on ${LINENO}"' ERR
>         trap 'echo "trap exit on ${LINENO}"' EXIT
> 
>         /bin/false
> 
>         exit 0
> 
>         You should get (and this is also the result you get in Bash 3.2):
> 
>         trap error on 6
>         trap exit on 8
> 
>         But you get:
> 
>         trap error on 6
>         trap exit on 1

This was changed in bash-4.0 with the rationale that traps, except the
special bash traps, should be run in a semi-independent environment with
a private value of $LINENO.  As a result, $LINENO gets reset before
running the trap.

I guess you could make a case to not reset $LINENO for the exit trap
due to this sentence in Posix (though $LINENO is not Posix):

"The environment in which the shell executes a trap on EXIT shall be
identical to the environment immediately after the last command executed
before the trap on EXIT was taken."

I'll have to think about it.

-- 
``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]