<story><title>An Opinionated Guide to Xargs</title><url>https://www.oilshell.org/blog/2021/08/xargs.html</url></story><parent_chain><item><author>fiddlerwoaroof</author><text>I frequently find myself reaching for this pattern instead of xargs:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    do_something | ( while read -r v; do
    . . .
    done )
&lt;/code&gt;&lt;/pre&gt;
I’ve found that it has fewer edge cases (except it creates a subshell, which can be avoided in some shells by using braces instead of parens)</text></item></parent_chain><comment><author>aaaaaaaaaaab</author><text>Some additional tips:&lt;p&gt;1. You don&amp;#x27;t need the parentheses.&lt;p&gt;2. If you use process substitution [1] instead of a pipe, you will stay in the same process and can modify variables of the enclosing scope:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    i=0
    while read -r v; do
        ...
        i=$(( i + 1))
    done &amp;lt; &amp;lt;(do_something)
&lt;/code&gt;&lt;/pre&gt;
The drawback is that this way `do_something` has to come after `done`, but that&amp;#x27;s bash for you ¯\_(ツ)_&amp;#x2F;¯&lt;p&gt;[1] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;www.gnu.org&amp;#x2F;software&amp;#x2F;bash&amp;#x2F;manual&amp;#x2F;html_node&amp;#x2F;Process-Substitution.html&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;www.gnu.org&amp;#x2F;software&amp;#x2F;bash&amp;#x2F;manual&amp;#x2F;html_node&amp;#x2F;Process-S...&lt;/a&gt;</text></comment>
<story><title>An Opinionated Guide to Xargs</title><url>https://www.oilshell.org/blog/2021/08/xargs.html</url></story><parent_chain><item><author>fiddlerwoaroof</author><text>I frequently find myself reaching for this pattern instead of xargs:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    do_something | ( while read -r v; do
    . . .
    done )
&lt;/code&gt;&lt;/pre&gt;
I’ve found that it has fewer edge cases (except it creates a subshell, which can be avoided in some shells by using braces instead of parens)</text></item></parent_chain><comment><author>ptspts</author><text>For thousands of arguments this sloution is much slower (high CPU usage) than xargs, because either it implements the logic as a shell script (slow) or it runs an external program for each argument (slow).</text></comment>