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

Re: Reading output into variables



On 04/07/2014 05:44 PM, Bart Schaefer wrote:
> ... 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.

My biggest issue with the "| read" built-in is that it doesn't work in
any shell beyond zsh (yes, I should have clarified that more).

Bash will still execute "read" as part of a separate pipeline, and thus
the variables are simply lost.

> } program | { read a b; hooray }
> 
> ... which leaves me uncertain as to what "subshell" you were talking
> about in the first example.

In this case of course, we just accumulate more commands in the same
subshell, so that we can temporarily use the values. I quite like the
idiom, as it makes the variables local to pipeline, but that's about it.

> The above "set" solution works in bash.  In zsh you could also do
> 
>     set -A array -- $(program)
> 
> to avoid clobbering $1 et al.

Yes, this goes in the direction that I wanted.

Another hack that I was able to use with both bash and zsh is by using a
here-string instead:

read a b <<< $(magic)

In the same line of thought

read a b < <(magic)

also works in bash.

I didn't think of using 'set'. This will work also in plash *sh, though
overwriting $1..N is not convenient, and at least "dash" doesn't support
arrays.

Now I'm left thinking what can I get out of plain ksh...




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