Consider the following very simple bash autocompletion specification.
#/usr/bin/env bash
_foo ()
{
local cur="${COMP_WORDS[$COMP_CWORD]}"
# The following line, when uncommented, results in broken behavior
local suggestions=($(compgen -W "$(./foo)" -- "$cur"))
# When the line above is commented out and the following line is uncommented, we get expected autocomplete behavior.
# local suggestions=('aaaa' 'bbbb' '--help')
echo -e "\n@@@ have ${#suggestions[@]} suggestions: ${suggestions[@]}"
COMPREPLY=("${suggestions[@]}")
}
complete -F _foo foo
When the above uses an external script to generate the autocomplete options using a simple echo "aaaa bbbb --help" online script stored in ./foo, then autocompletion seems to not work. When I type ./foo [TAB] I see the expected @@@ have 3 suggestions: aaaa bbbb --help. However, when I then press the second [TAB], only whitespace are printed to the output rather than the expected list of the three choices.
Conversely, when I instead declare the three choices inside my autocompletion script, I get the expected behavior. When I type ./foo [TAB] I see the expected @@@ have 3 suggestions: aaaa bbbb --help. Next, when I press the second [TAB], I see another line with @@@ have 3 suggestions: aaaa bbbb --help followed by the expected three choices --help aaaa bbbb on their own line.
Why isn't autocompletion script able to process the same choices when they are returned by my one-line script?