Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Bash's {} brace expansion syntax allows for creating easy permutations

# echo {b,c,d}{a,e,i,o,u}
ba be bi bo bu ca ce ci co cu da de di do du

However it's not clear to me if/how it's possible to use this along with arrays except in very awkward use of $() echo and eval

Is there a simple way to use arrays with curly brace (permutation) expansion?

For example sake, imagine something like (which does not work of course):

CONS=( b c d )
VOWEL=( a e i o u )

echo {${CONS[@]}}{${VOWEL[@]}}
share|improve this question
    
it's working in GNU bash, version 4.2.24 Output root@router:~# echo {${CONS[@]}}{${VOWEL[@]}} {b c d}{a e i o u}. if you want different then let us know .. ` –  Rahul Patil Sep 16 '13 at 21:18
2  
@RahulPatil the OP wants ba be bi bo bu ca ce ci co cu da de di do du. –  terdon Sep 16 '13 at 21:20
add comment

2 Answers

It's possible with zsh:

$ CONS=( b c d )
$ VOWEL=( a e i o u )
$ echo $^CONS$^VOWEL
ba be bi bo bu ca ce ci co cu da de di do du

Or es:

; VOWEL=( a e i o u )
; CONS=( b c d )
; echo $VOWEL^$CONS
ab ac ad eb ec ed ib ic id ob oc od ub uc ud

With bash or ksh93, you'd have to do something convoluted like:

VOWEL=( a e i o u )
CONS=( b c d )
qVOWEL=$(printf %q, "${VOWEL[@]}")
qCONS=$(printf %q, "${CONS[@]}")
eval "echo {${qVOWEL%,}}{${qCONS%,}}"
share|improve this answer
    
Just one more reason to use zsh. –  Kevin Sep 16 '13 at 22:08
    
Are you certain that there's no more direct way to do it in bash? Or do you just suspect that it is so? –  Catskul Sep 16 '13 at 22:09
    
@Catskul, no, I strongly doubt it but I'm not certain, I'm even less certain for ksh93 where a number of features are not documented. You may want to leave the question open for a while. I'm pretty sure you can't do it with brace expansion without another level of evaluation (with eval or .) though. –  Stephane Chazelas Sep 16 '13 at 22:14
add comment

You might use eval with IFS=,; "${array[*]}" (which joins the values with commas) or just two for loops:

$ CONS=(b c d);VOWEL=(a e i o u)
$ IFS=,;eval echo "{${CONS[*]}}{${VOWEL[*]}}"
ba be bi bo bu ca ce ci co cu da de di do du
$ for c in "${CONS[@]}";do for v in "${VOWEL[@]}";do echo "$c$v";done;done|paste -sd' ' -
ba be bi bo bu ca ce ci co cu da de di do du
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.