bug-bash
[Top][All Lists]
Advanced

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

Re: using exec to close a fd in a var crashes bash


From: Chet Ramey
Subject: Re: using exec to close a fd in a var crashes bash
Date: Mon, 21 Aug 2023 15:58:58 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.14.0

On 8/19/23 1:37 PM, jleivent wrote:

Bash Version: 5.2
Patch Level: 15
Release Status: release

Description:
         Using exec to close a file descriptor in a variable crashes
         bash.  It doesn't matter how the fd got into the variable, but
         the likely usage is with the {varname} form, as illustrated
         below.  I think what is happening is that the exec arg is being
         parsed as a target to execute instead of a fd redirection,

Correct. The

exec "${foo}"<&-

isn't a valid way to express it, in the same way that \2<&- doesn't
recognize the `2' as part of the redirection. The file descriptor and
redirection operator can't have any quoted portions. (Using the ${foo} or
$foo isn't a valid way to do it, either, since the file descriptor part of
the redirection has to be a number, unless you use the {ident} extension
as in your first command.)


         because when executed in two levels of shell (to allow the
         inner one to crash while leaving the outer), it generates the
         following error message:
                bash: exec: 10: not found

Yes, this is the indicator that that `10' isn't part of the redirection.


        But even that shouldn't cause the shell to terminate if the
        target isn't found, so maybe this is two bugs?

It doesn't terminate, per se. It successfully closes stdin and goes on,
as Greg noted. Since stdin is where an interactive shell is reading its
input, the subsequent read failure causes the shell to exit.
        
        Also note that the quoting and {} around foo in the last line
        don't impact the bug.

True, see above.


        This bug makes it difficult to use {varname} forms for file
        descriptors, as it means there is no easy way to close them
        before script exit.

Not true. `exec {foo}<&-' works fine.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/




reply via email to

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