I'm in a situation where I need to set up bash completion for a series of applications whose binaries all start with the same prefix -- let's call it ... blargh
. I do not and can not know which blargh*
applications a person using this completion program will have on their machine (although in my situation, the prefix is unique enough that it won't interfere with normal operation of other commands).
That is, I'd like to do something along the lines of:
complete -F _function_name "blargh*" "./blargh*"
Right now, what I've got is a pretty run-of-the-mill completion program:
# blargh-completions.bash
_blargh_completions () {
COMPREPLY=...
}
_blargh_init_completions () {
local blargh
for blargh in blarghABC blarghXYZ blargh123 blarghXXX-debug blarghARGH ; do
complete -F _blargh_completions "$blargh"
complete -F _blargh_completions "./$blargh"
done
}
_blargh_init_completions
Where I've got a decent initial guess at the application names, but I still have to instruct users to double-check the script and keep their local copy's for
loop updated as necessary, which isn't ideal.
Additionally, I'm a little unclear on where I'd install a completion script that handles multiple programs. All of the options have shortcomings:
- I could put it in compatdir (e.g.
/etc/bash_completion.d/
): This lets the script work with multiple commands, but at the same time all docs suggest this location is deprecated and should not be used. - I could put it in completionsdir (e.g.
/usr/share/bash-completion/completions/
): Except the script has to have the same name as the command in this directory so I don't know how to make it work in this case. - I could append it to the user's
~/.bash_completion
file, except then automated updates / uninstallations become difficult.
So my question is: How (if it's even possible) can I write and install a completion script that applies to a wildcarded name pattern like blargh*
without knowing the precise command names ahead of time?