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

Re: Capturing STDOUT without subshells or file I/O



Ben Klein wrote on Mon, 17 Sep 2018 16:13 -0400:
> So I guess the question is up again, how should I capture the `printf`
> output without the `-v` option, no subshells, and no file I/O? Is there
> a different method for ZSH v5.1?
> 
> I would like to do `printf '%.2f' "3.4" | read var` but it appears that
> the command before the pipe causes a subshell to be opened.
> 

If you could call pipe(2), you'd be able to do
.
    printf foo >&$w
    read bar <&$r
.
without forking (where $r,$w were returned by pipe(2)).  However, I
don't see any suitable callsite in the 5.6 codebase.  (Don't have 5.1
handy, sorry.)

Maybe we should expose pipe(2) in zsh/system?

> Or potentially, is there some way I can make a wrapper that will use
> `printf -v` when available, but falls back to another method?

Should be possible, yes.  You can tell the two cases apart by doing:

    if printf -v >/dev/null ; then

Cheers,

Daniel



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