Access bash tab completions from script

626 Views Asked by At

You normally interact with bash completion by pressing the tab key in your terminal. I would like to interact with it within a script of mine. Essentially, I would like a function/command that answers the question "If I pressed tab with text xyz already typed and the cursor as position n, what would be the suggestions?"

Does this function exist? I found compgen but it's poorly documented and doesn't seem to do what I want.

1

There are 1 best solutions below

6
AudioBubble On

The compgen seems to be exactly what you'd like to use anyway. To display what would happen if you'd be typing com and hitting the TAB key, use:

compgen -c com

Or:

compgen -A command com

This will output all Bash builtins along with external binaries and scripts found from the PATH.

More information can be found from the documentation in case you'd like to, for example, prune out from the output of compgen all Bash builtins and display just the external binaries/scripts.