zsh-users
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane@chazelas.org>
To: Ray Andrews <rayandrews@eastlink.ca>, Zsh Users <zsh-users@zsh.org>
Subject: Re: capture stderr in command substitution
Date: Mon, 25 Jan 2021 13:18:43 +0000	[thread overview]
Message-ID: <20210125131843.apg23u4xe3ru22pk@chazelas.org> (raw)
In-Reply-To: <20210125123800.5vn5zvtn5sho2rim@chazelas.org>

2021-01-25 12:38:00 +0000, Stephane Chazelas:
> 2021-01-24 11:45:31 -0800, Ray Andrews:
> > $ var=$(command)
> > 
> > ... but $var doesn't pick up errors.  Can it be forced?
> [...]
> 
> var=$(cmd>&2)
> 
> like in any Korn/POSIX-like shell.
[...]

D'oh sorry, that should be cmd 2>&1 (redirect fd 2 to the same
resource as fd 1 is redirected to (the writing end of the pipe
the other end of which is read by the shell to fillup $var)),
not cmd >&2 which would redirect stdout to the same resource as
fd 1 is redirected to, leaving none of cmd's file descriptor
connected to that pipe.

> To capture only stdout:
> 
> { var=$(cmd 2>&1 >&3 3>&-); } 3>&1
> 
> That is we need to redirect cmd's stdout back to what it was
> originally (after having redirected cmd's stderr to the pipe),
> using that extra fd 3 temporarily.
> 
> That's also standard sh syntax.
[...]

That one is correct.

-- 
Stephane


      reply	other threads:[~2021-01-25 13:19 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-24 19:45 Ray Andrews
2021-01-24 19:47 ` Axel Beckert
2021-01-25 15:15   ` Ray Andrews
2021-01-25 15:51     ` Vin Shelton
2021-01-25 16:28       ` Pier Paolo Grassi
2021-01-25 21:42         ` Bart Schaefer
2021-01-25 20:33       ` Ray Andrews
2021-01-25 12:38 ` Stephane Chazelas
2021-01-25 13:18   ` Stephane Chazelas [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210125131843.apg23u4xe3ru22pk@chazelas.org \
    --to=stephane@chazelas.org \
    --cc=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).