At xargs man page there is a usage example
cut -d: -f1 < /etc/passwd | sort | xargs echo
Why in this case echo is used with xargs, and sort without,
even both command has similar syntax where arguments follows command?
At xargs man page there is a usage example
cut -d: -f1 < /etc/passwd | sort | xargs echo
Why in this case echo is used with xargs, and sort without,
even both command has similar syntax where arguments follows command?
| connects the output of the left side with the input of the right side.
echo prints its arguments. It doesn't do anything with the data sent to its input. Therefore, xargs is used to call echo with the input.
sort, on the other hand, doesn't sort its arguments, but its input data.
Therefore, it makes sense to directly put input into sort.
(However, xargs echo really makes no sense. That really only converts the output of sort to arguments, just to convert these back to output. It's not quite sure what the intention here: cut -d: -f1 < /etc/passwd | sort would have worked just the same; ok, it would have a newline after every user, but in all honesty, this isn't a job for xargs, but for sed or awk, which would have directly done what the user would've wanted in the first place, on /etc/passwd; however, I'm pretty sure you're not very interested in the sensibility of that command line, more in why xargs is used that way.)
xargs echo will end up putting the sorted values all on a single line
xargs is being used, though
sed could've done the same). Anyways, I feel that's really not OP's focus, at all.
xargs will do that without echo’s help — sort | xargs is sufficient here. (Or sort | tr '\n' ' '.)