4

I have a bash function:

println(){echo "$1" "$2" "$3"}

and

export -f println

I want to use GNU parallel to run println. The command should take inputs of the following arrays:

a1=(a b c) 

a2=(x y)

a3=(z1 z2)

It should output the follows:

   a x a3
   a y a3
   b x a3
   b y a3
   c x a3
   c y a3

Which command can do that? I expect the command like:

 parallel ... println ... a1 ... a2 .... a3

Thank you!

2
  • 1
    a x a3 a y a3 Don't you want z1 z2 ?
    – KamilCuk
    Commented Jul 24, 2021 at 9:49
  • No, I take a1, a2 as data, a3 as argument to println.
    – kachi
    Commented Jul 25, 2021 at 11:29

2 Answers 2

5

Just put ::: in between:

$ parallel println ::: "${a1[@]}" ::: "${a2[@]}" ::: "${a3[@]}"
a y z1
a x z1
a y z2
a x z2
b x z2
b x z1
b y z2
b y z1
c x z2
c x z1
c y z1
c y z2

The println(){echo "$1" "$2" "$3"} is invalid, it needs to be println(){ echo "$1" "$2" "$3"; }.

2
  • Thank, great! Let me slightly adpat to: parallel println ::: "${a1[@]}" ::: "${a2[@]}" ::: "$a3". If one want to add more arguments to println, parallel println $arg1 $arg2 ... $argn ::: "${a1[@]}" ::: "${a2[@]}" ::: "$a3" works; otherwise, the first ::: can be removed?
    – kachi
    Commented Jul 25, 2021 at 11:32
  • @lidingxu Correct.
    – Ole Tange
    Commented Jul 26, 2021 at 12:33
1

Can be applied Brace Expansion in bash with variable IFS

(IFS=,; eval printf %s "{${a1[*]}}\ {${a2[*]}}\ {${a3[*]}}\$'\n'")
a x z1
a x z2
a y z1
a y z2
b x z1
b x z2
b y z1
b y z2
c x z1
c x z2
c y z1
c y z2

(...) - We execute in a subshell so as not to return the value to the IFS variable.

Or exactly like in your example:

(IFS=,; eval printf %s "{${a1[*]}}\ {${a2[*]}}\ a3\$'\n'")
a x a3
a y a3
b x a3
b y a3
c x a3
c y a3
2
  • Ok, but, there is no parallel.
    – kachi
    Commented Jul 25, 2021 at 11:33
  • Yes, then in your example the function is clearly superfluous parallel echo ::: ${a1[@]} ::: ${a2[2]} ::: ${a3[@]}
    – nezabudka
    Commented Jul 25, 2021 at 12:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .