Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: capture stderr in command substitution



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.

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.

-- 
Stephane




Messages sorted by: Reverse Date, Date, Thread, Author