I tried to use the xargs
to pass the arguments to the echo
:
[usr@linux scripts]$ echo {0..4} | xargs -n 1 echo
0
1
2
3
4
the -n 1
insured that the xargs
pass 1
arguments a time to the echo
.
Then I want to use this aruments twice, however the results is not I wanted:
[usr@linux scripts]$ echo {0..4} | xargs -I@ -n 1 echo @,@
0 1 2 3 4,0 1 2 3 4
the -n 1
seems disabled when I added the -I@
,
and this is the result I wanted:
0,0
1,1
2,2
3,3
4,4
how can I achieve that?
--------Supply------------------ I have used the method recommanded by @123 ,however ,there are still another question:
test.sh:
#!/bin/bash
a[0]=1
a[1]=2
echo "a[0] and a[1] : "${a[0]}, ${a[1]}
echo -n {0..1} | xargs -I num -d" " echo num,${a[num]},num
and this is the output:
[usr@linux scripts]$ sh test.sh
a[0] and a[1] : 1, 2
0,1,0
1,1,1
you can see that the array a
is not returned the value I wanted :<
And How can I fix this problem?