can xargs separate parameters?

why stick xargs? bash could handle this well:

p=(`echo "param1 param2"`); echo ${p[0]} ${p[1]}

echo "'param 1' 'param 2'" | xargs -n1 | xargs -I@ echo \[@\] \[@\]

(In my shell I need to escape [], your mileage may vary).


For those who find this from a search, the accepted answer did not work for me.

echo "'param 1' 'param 2'" | xargs -n1 | xargs -I@ echo \[@\] \[@\]

produces:

[param 1] [param 1]
[param 2] [param 2]

which does not meet the requirements given by the original poster to have xargs read in multiple entities, separate them, and send them to a single command ("echo" in the OP) as separate parameters. Xargs is not designed for this sort of task!


The bash answer can work.

p=(`echo "param1 param2"`); echo [${p[0]}] [${p[1]}]

produces:

[param1] [param2]

but this solution does not work with more than one line.


A correct solution with bash for sending pairs of lines as arguments to a single command is:

(echo 'param 1'; echo 'param 2'; echo 'param 3'; echo 'param 4') | while read line1; read line2; do echo "[$line1] [$line2]"; done

produces:

[param 1] [param 2]
[param 3] [param 4]


The GNU Parallel answer does work, but GNU Parallel must be make'd and installed. (The version packaged with Ubuntu is not GNU Parallel.)

Tags:

Linux

Shell

Xargs