It works:
$ p(){ eval echo $(printf "{%s,}" " $@ "); } $ pabcd abcd abc abd ab acd ac ad a bcd bc bd b cd cd
Or, more portable:
p() { [ $# -eq 0 ] && { echo; return; } ( shift; p " $@ " ) | while read a ; do printf '%b' "$1$a\n$a\n"; done } p " $@ "
Call it like this (use echo if you want a single line):
$ echo $(pabcde) abcde bcde acde cde abde bde ade de abce bce ace ce abe be ae e abcd bcd acd cd abd bd ad d abc bc ac c ab b a<br>
Note. It works for "complex" element values, just don't use echoes.
$ p " ab " "-c d-" "gg hh" ab -c d-gg hh -c d-gg hh ab gg hh gg hh ab -c d- -c d- ab
And, a non-recursive option (albeit slower) based on a binary representation of each value. This, however, is basically bash since it makes extensive use of arithmetic expansion.
#!/bin/bash powerset(){ [[ $# -eq 0 ]] && { echo "Missing set of arguments" >&2; exit 2; } local n ns; (( n=$#, ns=1<<n )) for (( i=1; i<ns ; i++ )); do a='';
Remove the comment character (#) after a='' if you need numbered results.
user2350426
source share