1

Is it possible to iterate over a list of letters, as followed:

aaaa, aaab, ..., aaaz, aaba, aabb, ..., aabz, ..., zzzy, zzzz

I know the syntax to iterate over the alphabet:

for i in {a..z} 

but couldn't figure out a way to do the extended version...

Thanks in advance

David Tzoor
  • 987
  • 4
  • 16
  • 33
  • Possible duplicate of [Looping through alphabets in Bash](https://stackoverflow.com/q/7300070/608639). – jww Dec 25 '19 at 12:06

3 Answers3

5

You could use brace expansion:

echo {a..z}{a..z}{a..z}{a..z}

Use it in a loop:

for i in {a..z}{a..z}{a..z}{a..z}; do
    echo $i
done

It would produce:

aaaa
aaab
aaac
aaad
aaae
...
zzzv
zzzw
zzzx
zzzy
zzzz

You can read more about combining and nesting brace expansions here.

devnull
  • 118,548
  • 33
  • 236
  • 227
  • Remember this is printing 26^4 lines though so may take a while and seem like its hung –  Jun 18 '14 at 07:54
1

Yes, BASH doesn't care what is contained in the sequence, as long as you give it the sequence.

for i in ducks geese swans; do echo $i; done
ducks
geese
swans

for building further with brace expansion, you just need to work on your brace statements:

for i in aaa{a..z}; do echo $i; done
aaaa
aaab
aaac
aaad
aaae
aaaf
aaag
aaah
aaai
aaaj
aaak
...

Take a look at brace expansion in man bash. You can use the above to satisfy your needs by a set of nested loops with differing levels of prefix for your expansion setups.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

I actually don't know bash well, but wouldn't that work?

// Pseudocode
for i in {a..z}
for j in {a..z}
for k in {a..z}
for l in {a..z}
echo $i$j$k$l
Spook
  • 25,318
  • 18
  • 90
  • 167