Skip to main content
2 of 2
added 176 characters in body
dotancohen
  • 16.5k
  • 28
  • 87
  • 123

Iterate over all values of a bash completion

Consider:

$ ssh fo<tab>
foo  fool  football

How would I write a for loop to iterate over those values?

#!/usr/bin/env bash
for SERVER in $(ssh fo<MAGIC HERE>) ; do echo $SERVER ; done

The list could change regularly, so hardcoding the values is not an option. In the specific case of SSH, I understand that I could grep the SSH config file for the matching hosts. But some other completion situations arise, such as:

$ git che<tab>
checkout      cherry        cherry-pick

The answer should be useful for these other ad-hoc completions as well.

dotancohen
  • 16.5k
  • 28
  • 87
  • 123