The fastest way to list every combination of 4 numbers

To find all combinations of 4 numbers in Bash, use nested for loops to iterate through your number range and print each combination. The simplest approach uses four loops, one for each position, and outputs every possible pairing without repeating the same set twice.

If you have 10 numbers and want every unique 4-number combination, you'll get 210 results. If you want every possible arrangement (where order matters), you'll get 5,040. The method you choose depends on whether you need combinations (order doesn't matter) or permutations (order does matter).

Key Takeaways

  • Nested for loops are the most straightforward way to generate combinations, with each loop handling one position in your 4-number set.
  • Use comparison operators like $i -lt $j to prevent duplicate combinations when order doesn't matter.
  • The seq command generates your number range, making it easy to change the starting point or total count.
  • Bash can handle thousands of combinations, but very large ranges (like 1 to 1000) will take several seconds to complete.

Generate combinations where order doesn't matter

This method produces unique combinations only — so 1-2-3-4 and 2-1-3-4 count as the same combination. Each number appears only once per set, and each set appears only once in your output.

Open your terminal and type this script:

#!/bin/bash for i in $(seq 1 10); do for j in $(seq $((i+1)) 10); do for k in $(seq $((j+1)) 10); do for l in $(seq $((k+1)) 10); do echo "$i $j $k $l" done done done done

Save this as combinations.sh, then run it with bash combinations.sh. The output will show every valid 4-number combination from 1 to 10, one per line. Each inner loop starts where the previous one ended (using $((i+1)), $((j+1)), and so on), which prevents duplicates.

To change your number range, replace the 10 in each seq command with your upper limit. To start from a different number, change the first 1 in the first seq command.

Generate permutations where order matters

If you need every possible arrangement — so 1-2-3-4 and 2-1-3-4 are different results — use this script instead:

#!/bin/bash for i in $(seq 1 10); do for j in $(seq 1 10); do for k in $(seq 1 10); do for l in $(seq 1 10); do echo "$i $j $k $l" done done done done

This version removes all the comparison operators and lets each loop run through the full range independently. With numbers 1 to 10, you'll get 10,000 results (10 × 10 × 10 × 10). This approach also allows the same number to appear multiple times in a single set — so 1-1-1-1 is a valid result.

Save output to a file instead of printing it

If your combination list is long, redirect the output to a file instead of printing to the screen. Add > output.txt to the end of your command:

bash combinations.sh > output.txt

This writes all results to output.txt in your current directory. You can then open it in a text editor or process it with other commands. To count how many combinations were generated, use wc -l output.txt.

Filter combinations by a specific condition

You can add an if statement inside the innermost loop to print only combinations that meet a certain rule. For example, to show only combinations that add up to 15:

#!/bin/bash for i in $(seq 1 10); do for j in $(seq $((i+1)) 10); do for k in $(seq $((j+1)) 10); do for l in $(seq $((k+1)) 10); do sum=$((i + j + k + l)) if [ $sum -eq 15 ]; then echo "$i $j $k $l (sum: $sum)" fi done done done done

The if [ $sum -eq 15 ] line checks whether the sum equals 15. Change -eq to -gt (greater than), -lt (less than), or -ne (not equal) to filter by different conditions. This approach works with any mathematical test you want to apply.

Speed up large number ranges

If you're working with a range larger than 20 numbers, the script will take noticeably longer to run. Bash processes each combination one at a time, so 1 to 50 can take 30 seconds or more. For very large ranges, consider using a compiled language like C or Python instead, which will run 10 to 100 times faster.

If you must use Bash, you can see progress by adding a counter. Insert this line after the first for loop:

echo "Processing i=$i" >&2

This prints the current value of i to the error stream (the &2 part), so it won't interfere with your output file. You'll see which iteration the script is on without mixing progress messages into your results.

Frequently Asked Questions

How do I change the number of positions from 4 to 3 or 5?

Remove or add a for loop. For 3 numbers, delete the innermost for l loop and its closing done. For 5 numbers, add a fifth loop after the fourth one. Each new loop should start at seq $((previous_variable+1)) 10) to avoid duplicates.

Why does my script produce duplicate combinations?

You're likely using the permutation script (where all loops run 1 to 10) when you need the combination script (where each loop starts after the previous one). Check that your second loop starts with seq $((i+1)), not seq 1.

Can I use letters instead of numbers?

Yes. Replace $(seq 1 10) with a list of your letters, like a b c d e f g h i j. The loops will iterate through each letter in the same way they iterate through numbers.

What's the difference between combinations and permutations?

Combinations treat 1-2-3-4 and 2-1-3-4 as the same result. Permutations treat them as different. Use the first script for combinations (fewer results) and the second script for permutations (many more results).