Just caught the tail of this thread so sorry for
chiming in from the peanut gallery...

        blah | while read x; do ...; done

behaves very differently to the for loop variant in that the body of the
loop runs in a sub-shell and thus cannot affect the outer scope.
In many cases that's exactly what you want.
Sometimes though, it isn't.

Conrad Meyer <c...@freebsd.org> wrote:
> On Tue, Dec 11, 2018 at 2:42 PM Devin Teske <dte...@freebsd.org> wrote:
> > In that case, would it be appropriate to say that:
> >
> >         blah | while read x; do ...; done
> >
> > Is always more efficiently written as:
> >
> >         IFS=$'\n'
> >         for x in $( blah ); do ...; done
> 
> I don't know.  The suggestion came from jilles@, who is much more
> familiar with sh(1) than I am.
> 
> My understanding is that it's important that 'set -o noglob' is set,
> or else 'blah' lines that include globs may be evaluated against the
> filesystem.  There is also a caveat if 'blah' is the 'set' command, or
> similar, in that IFS' own value itself will be split across multiple
> for loop iteration 'x' values ("IFS='", "'").
> 
> I would hesitate to say "always" given my limited understanding of the
> shell, but it might be true.
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to