Note: While rarely accessed directly, this is used by common helper scripts (albeit indirectly).
COMP_POINT
The index of the current cursor position relative to the beginning of the current command. If the current cursor position is at the end of the current command, the value of this variable is equal to ${#COMP_LINE}. Note: While rarely accessed directly, this is used by common helper scripts (albeit indirectly).
COMPREPLY
An array variable from which Bash reads the possible completions generated by a shell function invoked by the programmable completion facility.
Bash completion scripts generally only use COMP_WORDS and COMP_CWORD to determine the current and previous words on the command lines. However there are helper functions that use some of the more obscure variables like COMP_LINE and COMP_POINT. All in all, these are used to generate a list of completions which are stored in the array COMPREPLY and then printed to the terminal. So if we want the output of tab-completion, we need to access COMPREPLY after the completion script has populated it.
Here’s a very simple implementation:
That works alright, but what if things are more complicated? What if we want to perform programmatic tab-completion for other commands? For commands that do not load completions dynamically? For more complex command lines? What if we want to more accurately reflect real-world tab-completions?
Here is such a function:
Fox example get_completions 'apt-get ' outputs the following: