Re: Parameter assignment in a redirection word
"Mark J. Reed" <[email protected]> Sun, 26 Oct 2025 08:59:05 -0400
| Newsgroups | gmane.comp.shells.zsh.user |
|---|---|
| Message-ID | <CAA=-s3w2-PynmASnivFEbsBuMh6cL+ebphkkZqJxo-FpghwqqA@mail.gmail.com> |
Regardless of the rest, this construct:
$ (echo foo) | tee ${myvar:=tmpfile}
is expected to set the var in zsh (and also real AT&T ksh) but not bash.
That's because the right side of a pipe in bash is always in a subshell,
while the left side of the pipe is in the subshell in ksh and zsh.
But yeah, I can see no reason why the assignment would be subshelled in a
normal pipeless redirection from a subshell. Especially since the
subshellless version works, meaning it isn't getting subsumed by the "set
just for this command" behavior of bare assignments.
On Sun, Oct 26, 2025 at 5:17 AM Alexey Sukhoguzov <[email protected]> wrote:
> Hi,
>
> Please help me to clarify this situation:
>
> $ (echo foo) > ${myvar:=tmpfile}
> $ echo ${myvar:-still unset}
> still unset
>
> Why $myvar is set in a subshell instead of a parent process in this
> case? I was unable to find where it's documented, but I've checked
> some other shells, and of course it turned out that zsh is not alone
> here, at least bash is doing the same. OTOH, FreeBSD sh, oksh and
> dash all seem to set the variable in a parent. I was thinking that
> the latter is the only expected behavior, just like in
>
> $ echo foo > ${myvar:=tmpfile}
> $ echo ${myvar:-still unset}
> tmpfile
>
> which, by the way, works identically in all the shells that I've
> tested. But after I tried
>
> $ (echo foo) | tee ${myvar:=tmpfile}
>
> and found that $myvar is set by a parent in zsh only, I'm not sure
> what to expect any longer. Could anyone shed some light on what's
> going on here?
>
>
--
Mark J. Reed <[email protected]>