I want to use the aruments from the xargs as the index of this array, this is the scripts:
1 #!/bin/bash 2 array[0]=x 3 array[1]=y 4 echo array : ${array[0]}, ${array[1]} 5 echo -n {0..1} | xargs -I index -d" " echo index,${array[index]} and this is the output:
[usr@linux scripts]$ sh test.sh array : x, y 0,x 1,x you can see that the array can not accept the index correctly, it's always the first. How can I get this kind of output:
array : x, y 0,x 1,y I showed the example with the command echo, however, my real aim is for another command, like this :
echo -n {0..1} | xargs -I index -d" " somecommand ${array[index]} so that I want a general solution of this question.
And I also tried the parallel instead of the xargs, it's have the same problem.
xargsworks by creating child processes. The${...}stuff in the xargs command line is expanded only once, before xargs is executed. You'll have to either make the array available to child processes, or rewrite the xargs as a shell loop.xargsto do parallel, so a shell loop could not be a proper solution :(xargs, but there could be corner cases...