Get all possible combinations of a word in lower/uppercase letters
A slightly better solution:
echo {h,H}{a,A}{r,R}{l,L}{e,E}{y,Y}
For full scalability:
echo harley \
| perl -nle 'print "echo ",
join "",map { "{" . lc . "," .uc ."}" } split //' \
| xargs -I {} bash -c "{}"
If you absolutely must have one word per line, go with
for w in {h,H}{a,A}{r,R}{l,L}{e,E}{y,Y};do echo $w;done
thanks to mattdm's comment
The corresponding scalable version would be:
echo harley \
| perl -nle 'print join "",map { "{" . lc . "," .uc ."}" } split //' \
| xargs -I {} bash -c 'for w in {};do echo $w;done'
For fun, try replacing "harley" with "supercalifragilisticexpialidocious" It's been 5 minutes and my computer is still crunching on this one and will probably never finish :)
eval echo $(echo "word" | sed 's/./{\U&,\L&}/g')
sed 's/./{&,&}/g'
would turnFoo
into{F,F}{o,o}{o,o}
, which would be pretty useless. But add\U
and\L
and you get the upper and lower case of each letter; i.e.,{F,f}{O,o}{O,o}
.- Then it’s a simple matter of using
eval
to tell the shell to expand the {X,x} brace sequences.
EDIT 2: This answer is wrong. It doesn't produce 2^n combinations as it should.
EDIT: I don't know why, but this solution is realy fast compared to the perl solution by @Joeseph R. It runs "Supercalifragilisticexpialidocious" in less than 0.3 seconds!
Here's my crack at it:
#!/bin/bash
str=${1^^} # convert to uppercase
len=${#str} # get length of string
for ((perm=0; perm <= len; perm++)); do
for ((i=0; i <= len; i++)); do
lower=${str,,} # convert to lowercase
# Uppercase n-th letter for permutation
if [ $perm -gt 0 ]; then
nth=${lower:perm-1}
lower=$(echo ${lower:0:perm-1}${nth^})
fi
echo -n ${str:0:i} # print orig string from 0 to $i
echo ${lower:i} # print new string from $i to end
done
done | sort -u
Running it:
$ ./permutations.sh hi
hi
hI
Hi
HI
$ ./permutations.sh harley
harley
harleY
harlEy
harLey
haRley
hArley
Harley
HarleY
HarlEy
HarLey
HaRley
HArley
HArleY
HArlEy
HArLey
HARley
HARleY
HARlEy
HARLey
HARLeY
HARLEy
HARLEY
Feel free to fork and modify it, I'm sure it can be optimized. https://gist.github.com/ryanmjacobs/4c02ad80f833dee0c307