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

Re: Reading output into variables

On Apr 7,  4:20pm, Yuri D'Elia wrote:
} Maybe a stupid question, but is there a way to read the output of a
} program and split into variables _conveniently_ using the IFS?

Like Peter, I'm a little confused about what you're asking.

A basic constraint of the way that I/O works in unix-like systems is
that you have to use either the filesystem or a separate process to
"read the output" of anything without danger of deadlock.  So when
you ask for ...

} program | read a b
} minus the subshell.

... it would seem to imply that "program" is a shell construct so it
doesn't need an extra process in the first place, which leaves a temp
file as the only answer to your question ... but that probably isn't
as "convenient" as you want.

But then you give this example ...

} program | { read a b; hooray }

... which leaves me uncertain as to what "subshell" you were talking
about in the first example.

Perhaps you mean something like

    set -- $(program)

which still uses a subshell but splits on IFS into $1 $2 etc.  Which
means you have to be done using those as program / function arguments.

} Bonus points if that's something that would also work in bash.

The above "set" solution works in bash.  In zsh you could also do

    set -A array -- $(program)

to avoid clobbering $1 et al.

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