On 2023-08-23 at 10:34 -0400, Greg Wooledge wrote: > 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.
You can just toss out shopt -s varredir_close 2> /dev/null || true # Close varredir_close on bash 5.2+ at the top of the script, and have correct behavior with recent bash, and small-fd-leak-but-otherwise-working on previous versions that are 4.1+.