bug-bash
[Top][All Lists]
Advanced

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

Re: Failed $(<file) exits under 'set -e' even when followed by ||


From: Emanuele Torre
Subject: Re: Failed $(<file) exits under 'set -e' even when followed by ||
Date: Mon, 3 Apr 2023 16:43:40 +0200
User-agent: Mutt/2.2.10 (2023-03-25)

On Mon, Apr 03, 2023 at 10:30:29AM -0400, Chet Ramey wrote:
> On 3/31/23 10:20 PM, Zev Weiss wrote:
> 
> > Bash Version: 5.2
> > Patch Level: !PATCHLEVEL!
> > Release Status: release
> > 
> > Description:
> > 
> > When 'set -e' is enabled, file-content substitutions of the form $(<...)
> > cause an immediate exit even when the subsitution is in a conditional
> > context such as the condition of an 'if' statement or the left operand
> > of the '||' operator.
> 
> It's always been a (fatal) redirection error, but, as you suspect, has a
> different effect now that $(<file) is run without forking. I'll change it
> to a non-fatal failed word expansion for this case.

Very interesting.

I agree that it makes more sense as a non-fatal shell error.

I am a bit surprised that the problem only occurs when errexit is on,
and not when it is off though.

In any case, a possible workaround, for now, is to use the usual
technique to "catch" fatal errors:

    # in standard mode
    if ! eval 'x=$(</nosuchfile)'; then
        x=''
        echo hello
    fi

Or:
    # in POSIX mode or standard mode
    if ! command eval 'x=$(</nosuchfile)'; then
        x=''
        echo hello
    fi

 emanuele6



reply via email to

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