On Wed, Aug 23, 2023 at 1:48 AM Martin D Kealey <[email protected]>
wrote:
> Chopping and changing behaviour in "permanent" releases creates a
> maintenance nightmare.
>
Well now with Bash 5.2, we've got the varredir_close shell option,
something savvy shell programmers would probably just always use, like
lastpipe.
>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.
In the Bash man page:
varredir_close
If set, the shell automatically closes file descriptors assigned
using the {varname} redirection syntax (see REDIRECTION above) instead of
leaving them open when the command completes.
I feel like the man page would benefit from that caveat about exec. I felt
the need to test that bit myself. "Surely not."
$ shopt varredir_close
varredir_close off
$ ls /dev/fd
0 1 2 3
$ printf 'words\n' {fd}>&1 1>&2 2>&$fd {fd}>&-
words
$ ls /dev/fd
0 1 10 2 3
$ exec {fd}>&-
$ ls /dev/fd
0 1 2 3
$ shopt -s varredir_close
$ printf 'words\n' {fd}>&1 1>&2 2>&$fd {fd}>&-
words
$ ls /dev/fd
0 1 2 3
$ exec {fd}> this_file.txt
$ printf 'words\n' >&$fd
$ printf 'other words\n' >&$fd
$ exec {fd}>&-
$ cat this_file.txt
words
other words
Zack
[1]: https://git.savannah.gnu.org/cgit/bash.git/tree/NEWS