13

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?

a06e
  • 18,594
  • 33
  • 93
  • 169

2 Answers2

14

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

parallel echo ::: ${x[@]}
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • 8
    I think it would be better to use `"${x[@]}"` in case any array elements contain spaces in future, e.g. `x=("0.1 + 6" "0.2 - b" "0.5 + a")` – Mark Setchell Mar 16 '16 at 20:40
5

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
Ole Tange
  • 31,768
  • 5
  • 86
  • 104