You could use brace expansion. But it's ugly. You need to use eval
, since brace expansion happens before (array) variable expansion. And "${var[*]}"
with IFS=,
to create the commas.
Consider a command to generate the string
echo {a,b,c}+{1,2,3}
Assuming the arrays are called letters
and numbers
, you could do that using the "${var[*]}"
notation, with IFS=,
to insert commas between the elements instead of spaces.
letters=(a b c)
numbers=(1 2 3)
IFS=,
echo {"${letters[*]}"}+{"${numbers[*]}"}
Which prints
{a,b,c}+{1,2,3}
Now add eval
, so it runs that string as a command
eval echo {"${letters[*]}"}+{"${numbers[*]}"}
And you get
a+1 a+2 a+3 b+1 b+2 b+3 c+1 c+2 c+3