Reputation: 354
I'm not sure how to word it (I'm having one of those moments where I forget everything), but I'd like to create a script where it gives me every word possible made up from characters.
For example, if I wanted a variety of "spaces", I could input (s|S)/(p|P)/(A|a|4)/(C|c)/(e|3|E|ε)/(s|S|5|$)
into the script, and in return get an output like this:
...
sP4C3s
SpaCe5
spACεS
Sp4C3$
...
So on, so forth. How could I create that?
(Tried searching for this by the way, but couldn't find anything that helped. It's probably me wording it wrong.)
Upvotes: 0
Views: 28
Reputation: 246847
You would use a brace expansion in bash:
echo {s,S}{p,P}{a,A}{c,C}{e,E}{s,S}
spaces spaceS spacEs spacES spaCes spaCeS spaCEs spaCES spAces spAceS spAcEs spAcES spACes spACeS spACEs spACES sPaces sPaceS sPacEs sPacES sPaCes sPaCeS sPaCEs sPaCES sPAces sPAceS sPAcEs sPAcES sPACes sPACeS sPACEs sPACES Spaces SpaceS SpacEs SpacES SpaCes SpaCeS SpaCEs SpaCES SpAces SpAceS SpAcEs SpAcES SpACes SpACeS SpACEs SpACES SPaces SPaceS SPacEs SPacES SPaCes SPaCeS SPaCEs SPaCES SPAces SPAceS SPAcEs SPAcES SPACes SPACeS SPACEs SPACES
If you want the words newline-separated, use
printf "%s\n" {s,S}{p,P}{a,A}{c,C}{e,E}{s,S}
To make this reusable, put it in a function. However, since brace expansion is the first expansion performed by the shell, you can't use variables in it without using eval
:
casecombinations() {
local source brace_expr i char
for source in "$@"; do
brace_expr=""
for ((i=0; i<"${#source}"; i++)); do
char="${source:i:1}"
case $char in
[[:alpha:]]) brace_expr+="{${char,},${char^}}";;
*) brace_expr+="\\$char";;
esac
done
eval echo "$brace_expr"
done
}
casecombinations hello world
Upvotes: 2