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

Re: Capture stderr to variable without new process



On 2013-01-22 at 01:41 +0530, rahul wrote:
> I guess the typical way would be:
> 
>    ERR=$(popd 2>&1)
> 
> However, the command cannot run in a sub-shell as it would have no effect
> in the current shell.
> Is writing to a file the only way ?

If the output can only be one line and you have an unbuffered cat(1) (-u
option).

% coproc cat -u
[1] 6550
% popd 2>&p
% read -p foo
% echo $foo
popd: directory stack empty
% coproc -

Otherwise, you need to script a cat-command which can exit on a sentinel
line; I don't know of a way to close the coproc's stdin while leaving
the coproc running to collect its output later.  Perhaps someone else
does?  If you could send EOF on the coproc's stdin, then you could just
use:
  popd_stderr="$(cat <&p)"
after closing its stdin.

-Phil



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