The quick answer
Programmable completion functions inspect the current command line and populate COMPREPLY with candidate completions.
COMP_WORDS COMP_CWORD COMPREPLY
What's going on?
Understanding the underlying mechanism makes troubleshooting much easier than memorising a command in isolation.
Dig a little deeper
COMP_WORDS is an array of parsed words; COMP_CWORD is the index of the word under the cursor; COMPREPLY is the output array. COMP_LINE and COMP_POINT help with finer-grained parsing.
Hands-on commands
Try these in a suitable test environment. Read each command before running it, especially if you have elevated privileges.
complete -p | head
help complete
help compgen
One more thing...
Tab is a tiny key with enormous ambitions.
Explore another mystery ↗Further reading
Want the command details, caveats and operational checks? The reference library has a longer technical guide.
Read the detailed reference →