I am currently using ubuntu18.04 server and the snap install of LXD. I wrote a short script that runs a few basic lxc commands to manage my containers, the script works fine. However, unlike the individual lxc commands my script cannot autocomplete container names. Is there some way I can set up autocompletion of lxc container names for my bash script?
The bash completion script effectively runs something like lxc list --format=csv -c n and tells bash to complete from it. You may be able to use a similar approach using the provided name and some grepping on that list output.
I inserted the below function into the “/usr/share/bash_completion/bash_completion” file.
_foo()
{
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W “$(lxc list --format=csv -c n)” – $cur) )
}
complete -F “_foo” bashscript.sh
The above seems to be working well, will update if any issues arise.
this does noting for me. replacing foo with lxc did nothing either.
Is there anything beyond source /usr/share/bash-completion/completions/lxc (the name of the script on my Manjaro machine which did not exist) that needs to be done?