Can bash completion be invoked programmatically? Can bash completion be invoked programmatically? bash bash

Can bash completion be invoked programmatically?


I actually had to do this to figure out how apt-get autocomplete works on ubuntu (built my own pseudo-repository tool :)

This is a multistep process:

First, complete -p will give you a listing of all completions in the form of a set of commands you can run to replicate the configuration. For example, lets say you want to hunt down the autocomplete for apt-get. Then:

$ complete -p | grep apt-getcomplete -F _apt_get apt-get

This tells you that the shell function _apt_get is called by the completion mechanism.

You need to recreate the special variables used by the function,, namely COMP_LINE (the full line), COMP_WORDS (bash array of all of the arguments -- basically split COMP_LINE), COMP_CWORD (index, should point to last value), COMP_POINT (where within the word you are doing the autocomplete), and COMP_TYPE (this is how you tell it that you want to complete as if you hit tab).

Note: read the manpage for more info -- this is how i figured it out in the first place. man bash