xargs can come in handy in a pinch, but it's usually unnecessary and the way it generates arguments can cause some complications. I can't think of a case where xargs would be more appropriate than Command Substitution or a while read loop, e.g.:
open "$(some_commands)"#thus you fully control word-splitting
or
some_commands | \while IFS=read-r line;do#unsetting IFS here prevents word-splitting
some_other_commands "$line"
even_some_more_commands "$line"#vars declared in here are not valid afterwards,#since they are declared inside the pipeline subshell#and its environment does not hoist#This includes $linedone#alternatively, using process substitution:while IFS=read-r line;do
some_other_commands "$line"#variables set here remain valid afterwards#since this while-loop is not in a subshell#thus the last $line will remain set afterwardsdone < <(some_commands)
With process substitution and the mapfile builtin, you can even populate an array and iterate over that:
xargs
can come in handy in a pinch, but it's usually unnecessary and the way it generates arguments can cause some complications. I can't think of a case wherexargs
would be more appropriate than Command Substitution or awhile read
loop, e.g.:or
With process substitution and the
mapfile
builtin, you can even populate an array and iterate over that: