On Sun, May 22, 2016 at 1:06 PM, Chet Ramey <chet.ra...@case.edu> wrote: > On 5/21/16 5:16 PM, adonis papaderos wrote: > >> Bash Version: 4.3 >> Patch Level: 42 >> Release Status: release >> >> Description: >> When using redirections preceded by a word on builtins >> 'i.e. : {varname}<&1' >> the redirection persists to the current shell. > > This is by design, and has been discussed previously. One such discussion > starts at http://lists.gnu.org/archive/html/bug-bash/2016-01/msg00156.html. > Maybe it should only do it when preceded by `exec', but it never has. > >> Later using {varname}<&- does not close fd. > > You don't precede it with an `exec', so it doesn't affect the current > shell. So-called `null' commands with redirections are executed in > subshells. This is where you need the `exec'.
I know POSIX allows for a subshell but don't know what the point is. I was confused by this when I first hit it. The subshell isn't obvious. $ bash -c 'echo "$BASHPID" {x[$((pid=BASHPID))$(echo "2: $pid" >&2),0]}<<<"$((pid=BASHPID))$(echo "1: $pid" >&2)"' 3907 1: 3908 2: 3908 $ bash -c 'echo "$BASHPID" {x[$((pid=BASHPID))$(echo "2: $pid" >&2),0]}<<<"$((pid=BASHPID))$(echo "1: $pid" >&2)"' 3911 1: 3911 2: 3911 ksh being the only other shell that supports auto-fd assignment for null commands doesn't create one. $ bash -c 'typeset -li y=2 {x[y*=2]}<&$((y*=2,0)); typeset -p x y' bash: line 1: typeset: x: not found declare -il y="2" $ bash -c 'typeset -li y=2 : {x[y*=2]}<&$((y*=2,0)); typeset -p x y' declare -a x='([8]="10")' declare -il y="8" $ ksh -c 'typeset -li y=2 {x[y*=2]}<&$((y*=2,0)); typeset -p x y' typeset -a -i x=([8]=11) typeset -l -i y=8 $ ksh -c 'typeset -li y=2 : {x[y*=2]}<&$((y*=2,0)); typeset -p x y' typeset -a -i x=([8]=11) typeset -l -i y=8 ksh and zsh are hard to test since they do a horrible job parsing the {fd} in all but trivial cases. It would be way simpler to only allow redirects at the end of a command and require each redirect to be a separate word. That eliminates a ton of ambiguities for {fd}>x, &>x and >&x. Maybe add a `set -k` work-alike for legacy scripts and people that insist on `echo >&2 i like surprise redirec{t}<ion`.