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

Re: Capturing STDOUT without subshells or file I/O



On 18 Sep, Sebastian Gniazdowski wrote:
> > I have an interesting predicament, I with to capture the STDOUT (builtin printf) of a ZSH function to a variable, in which the function should be running in the current shell context.
> mksh can do this with:
>
> ${ foo;}
>
> foo runs in current shell and the ${ ... } is substituted with its
> output.

I was under the impression that ksh93 does this with the basic $( ... )
syntax meaning that that syntax does not inherently imply the use of
a subshell. It depends on whether there are pipes, external commands or
only builtins there.

For ksh93, they had their own I/O abstraction – sfio – which was a
complete replacement for stdio but which made it easy to capture the
output instead of writing it. Using sprintf everywhere would be a
very invasive change but the open_memstream/temporary file approach
used for print -v would work. And in any case, there would be other,
bigger, problems to solve.

Personally, I can't claim to be have ever been especially concerned
about the cost of the fork and was not especially enthused by the
addition of -v to print.

$(...) substitutions are also run sequentially:

  echo $SECONDS ; echo $(sleep 1;echo $SECONDS) $(sleep 1;echo $SECONDS)

Perhaps I'm forgetting something obvious but at the moment, I can't
think why they couldn't be run in parallel - if performance is the
concern.

Oliver



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