bug-bash
[Top][All Lists]
Advanced

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

Re: bash drops errexit option in sourced file


From: Chet Ramey
Subject: Re: bash drops errexit option in sourced file
Date: Mon, 03 Dec 2012 10:25:54 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:16.0) Gecko/20121010 Thunderbird/16.0.1

On 12/3/12 4:12 AM, Robert Schiele wrote:

> Bash Version: 4.2
> Patch Level: 24
> Release Status: release
> 
> Description:
> While normally sourced files have identical options set like the
> invoking environment this is not true if the sourcing is done from
> within a function foo that was called with something like:
> 
> foo && true
> 
> The following example demonstrates this:
> 
> $ cat bug
> #!/bin/bash
> set -e
> 
> int()
> {
>     echo A: $-
>     . source
>     echo D: $-
> }
> 
> echo Broken:
> int && true
> 
> echo OK:
> int
> $ cat source
> echo B: $-
> false
> echo C: $-
> $ bash bug
> Broken:
> A: ehB
> B: hB
> C: hB
> D: ehB
> OK:
> A: ehB
> B: ehB
> $
> 
> This error showed up in bash 4.0 first and was not present in 3.2 or older.
> 
> As you can see in the invocation "int && true" the flag is still set
> inside the function but magically disappears once the file is sourced,
> while in the second incocation "int" everything behaves as expected.
> Apparently something gets broken when the exit on error is temporarily
> disabled for the chained command "int && true" because while it gets
> properly restored within the function itself it disappears again when
> sourcing the other file.

Since the `source' command is called in a context where all commands within
it should have the `errexit' flag disabled, bash chooses to satisfy this
requirement by turning off the flag that (internally) represents errexit.
Under most circumstances, however, that state is externally visible as the
value of the -e option.

Note that the behavior of suppressing the effect of the errexit option is
correct.  The value of the -e option in $- should be unaffected, though.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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