FG
💻 Software

How to iterate over all pair combinations in a list in bash?

Fresh5 days ago
Mar 15, 202615614 views
Confidence Score0%
0%

Problem

I define a list in bash like this: and I want to do a loop where I iterate through all possible pair combinations. In Perl, I would use a while/foreach with a shift on the list like this: I don't want the same element in the pair and don't care about the order of the pairs, so if the list is "A B C…

Error Output

list="element1 element_2 my_element_3 element04"

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: How to iterate over all pair combinations in a list in bash?

Low Risk

The simplest approach seems to be to use positional parameters.

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment