Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Feed GNU parallel with an array?

How to feed a command in GNU parallel with an array? For example, I have this array:

x=(0.1 0.2 0.5)

and now I want to feed it to some command in parallel

parallel echo ::: $x

This does not work. It is feeding all the arguments to a single call, since it prints

0.1 0.2 0.5

instead of

0.1
0.2
0.5

which is the output of

parallel echo ::: 0.1 0.2 0.5

How can I do it right?

like image 782
becko Avatar asked Mar 16 '16 17:03

becko


2 Answers

If you want to provide all the elements in the array use:

parallel echo ::: ${x[@]}
like image 103
Diego Torres Milano Avatar answered Sep 21 '22 21:09

Diego Torres Milano


From: http://www.gnu.org/software/parallel/man.html

EXAMPLE: Using shell variables When using shell variables you need to quote them correctly as they may otherwise be split on spaces.

Notice the difference between:

V=("My brother's 12\" records are worth <\$\$\$>"'!' Foo Bar)
parallel echo ::: ${V[@]} # This is probably not what you want

and:

V=("My brother's 12\" records are worth <\$\$\$>"'!' Foo Bar)
parallel echo ::: "${V[@]}"

When using variables in the actual command that contains special characters (e.g. space) you can quote them using '"$VAR"' or using "'s and -q:

V="Here  are  two "
parallel echo "'$V'" ::: spaces
parallel -q echo "$V" ::: spaces
like image 30
Ole Tange Avatar answered Sep 19 '22 21:09

Ole Tange