What you're doing when you generate combinations

When you generate all combinations of 4 numbers in Bash, you're telling the terminal to create every possible arrangement of those numbers and print them out. If you have the numbers 1, 2, 3, and 4, the script will produce 1234, 1243, 1324, 1342, 1423, 1432, and so on — every single ordering. This is useful when you need to test passwords, check all possible orderings of data, or understand how many ways something can be arranged.

Bash does this work through loops — you stack one loop inside another, each one cycling through your available numbers. The innermost loop changes fastest, the outermost changes slowest, and together they produce every combination.

Key Takeaways

  • Four nested loops, each cycling through your number range, will generate all combinations without needing extra tools.
  • The simplest approach uses for loops stacked inside each other, with each loop handling one position in your 4-number sequence.
  • You can limit the numbers you use (1 through 9, or 0 through 3) by changing the range in each loop's curly braces.
  • The echo command at the innermost level prints each complete combination as the loops cycle through.
  • Running the script in your terminal requires saving it to a file, making it executable with chmod +x, then running it with ./filename.

The four nested loops method

The most straightforward way to generate combinations is to write four for loops, one inside the other. Each loop represents one position in your 4-number sequence. Here's the basic structure:

Open a text editor and type this:

#!/bin/bash for a in {1..9}; do   for b in {1..9}; do     for c in {1..9}; do       for d in {1..9}; do         echo "$a$b$c$d"       done     done   done done

The first line, #!/bin/bash, tells the system this is a Bash script. Each for loop cycles through the numbers 1 to 9 (the range {1..9}). The variable a holds the first number, b the second, c the third, and d the fourth. The echo command at the center prints all four variables together as a single number. Each time the innermost loop completes, the loop above it advances, and the cycle repeats until all combinations are printed.

Changing the number range

The range {1..9} means "start at 1 and go up to 9". You can change this to any numbers you want. If you need combinations from 0 to 3, replace {1..9} with {0..3}. If you need 1 to 5, use {1..5}.

You can also use different ranges in different loops. For example, if the first number should only be 1 or 2, but the others can be 1 through 9, change the first loop to for a in {1..2}; do and leave the others as {1..9}. This gives you control over which positions accept which numbers.

Saving and running your script

Save the script with a name like combinations.sh. In most text editors, use File > Save As, choose a location you can find (your home folder is easiest), and type the filename.

Open the terminal and navigate to the folder where you saved the file. If you saved it in your home folder, type cd ~ and press Enter. Then type ls and press Enter to see the files in that folder and confirm your script is there.

Next, make the script executable by typing chmod +x combinations.sh and pressing Enter. This tells the system the file is allowed to run. Then type ./combinations.sh and press Enter. The terminal will print every combination, one per line.

Limiting output with head or piping to a file

If you use the range {1..9} for all four loops, the script will print 6,561 combinations (9 × 9 × 9 × 9). That's a lot of text scrolling past. To see only the first 20, type ./combinations.sh | head -20. The pipe symbol | sends the output to the head command, which prints only the first 20 lines.

To save all combinations to a file instead of printing them to the screen, type ./combinations.sh > combinations.txt. The > symbol redirects the output into a file called combinations.txt. You can then open that file in a text editor or process it with other commands.

Understanding why this works

Each loop cycles independently. The outermost loop (variable a) changes once for every 729 combinations (9 × 9 × 9). The next loop (variable b) changes once for every 81 combinations (9 × 9). The third loop (variable c) changes once for every 9 combinations. The innermost loop (variable d) changes with every single combination. This nesting ensures that every possible ordering appears exactly once.

If you trace through a small example with {1..2} for all loops, you get: 1111, 1112, 1121, 1122, 1211, 1212, 1221, 1222, 2111, 2112, 2121, 2122, 2211, 2212, 2221, 2222. That's 16 combinations (2 × 2 × 2 × 2), and every ordering of 1 and 2 across four positions appears.

Frequently Asked Questions

What if I want combinations of 3 numbers instead of 4?

Remove one loop. Keep only three nested loops with variables a, b, and c, and change the echo line to echo "$a$b$c". For 5 numbers, add a fifth loop and include that variable in the echo command.

Can I use letters instead of numbers?

Yes. Replace {1..9} with a list like {a..z} or {A..Z}, or write out specific letters: for a in a b c d; do. The loops work the same way with any characters.

Why does my script print nothing?

The most common reason is that the file is not executable. Run chmod +x combinations.sh again. If that does not work, check that the filename matches what you typed — if you saved it as combinations.sh but typed ./combination.sh, the system will not find it.

How do I stop the script if it's printing too much?

Press Ctrl+C (hold Control and press C). This stops any running command in the terminal when ready.

Can I generate combinations without repeating numbers?

The nested loop method shown here allows repeats (like 1111 or 1123). To prevent repeats, you need conditional logic that checks whether each new number has already been used — this requires an if statement inside each loop to skip numbers already chosen. That approach is more complex and slower for large ranges.