I am trying to set up bash completion for a utility script I wrote, so I added the following script to /etc/bash_completion.d:
_mcd()
{
local cur words
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
words=`mcd-completion-words`
COMPREPLY=( $(compgen -W "${words}" -- "$cur") )
return 0
}
complete -F _mcd mcd
The mcd-completion-words
script programmatically finds available operators for the command. When I restart bash (or source the script), I can successfully tab complete, but if I do so, I can no longer backspace past a completed character.
Also, if I attempt to list all options (e.g. I attempt to tab complete w/ no word in place), bash adds a tab to the command, which I also cannot backspace.
How can I make bash mimic normal file completion behavior? Any help is appreciated. Thanks!
Here's a reduced testcase for mcd-completion-words that still exhibits the same behavior. Curiously, Dennis' case works for me as well (when substituting in words="one two three", for example).
#!/usr/bin/env php
<?php
print "one two three four five six seven eight nine";