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: Greg Wooledge
Subject: Re: using exec to close a fd in a var crashes bash
Date: Wed, 23 Aug 2023 10:34:33 -0400

On Wed, Aug 23, 2023 at 09:50:36AM -0400, Zachary Santer wrote:
> From the NEWS file [1]:
> 
> o. The new `varredir_close' shell option causes bash to automatically close
>    file descriptors opened with {var}<fn and other styles of varassign
>    redirection unless they're arguments to the `exec' builtin.

Hmm, interesting.

> $ shopt -s varredir_close
> $ printf 'words\n' {fd}>&1 1>&2 2>&$fd {fd}>&-
> words
> $ ls /dev/fd
> 0  1  2  3

Of course, you can drop the {fd}>&- there, because it does nothing.  With
or without varredir_close, either way, it does nothing.

So... {var}> redirections were introduced in bash 4.1, and varredir_close
in bash 5.2.  That means that in all versions from bash 4.1 to 5.1, you
will need the separate "exec {fd}>&-" to close the temp FD.  At this
point, it hardly seems worthwhile to make use of a feature that only
works in bash 5.2, which is surely a tiny fraction of the set of installed
bash instances in the world.

Perhaps in a decade or so, when bash 5.2+ instances are the majority,
it will make sense to expect that feature to exist.  But you'd still
need fallback code for the rest.

Then again... leaving an FD open in a shell script usually won't matter,
because the script will exit, and that'll just take care of it.  The
only times it actually matters are long-running bash processes -- either
interactive shells, or some kind of weird daemon that's been implemented
in bash for some reason -- or scripts that open and (fail to) close lots
of temp FDs in a loop.

So, unless you're using this feature in an interactive shell function
or programmable completion or something, I guess it can mostly be ignored.



reply via email to

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