16

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?

2 Answers 2

21

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

parallel echo ::: ${x[@]}
Sign up to request clarification or add additional context in comments.

1 Comment

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")
7

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.