LXD container name autocomplete with bash script

Hello,

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?

Thank you

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.

Thanks for the reply,

I ended up combining your response with this “http://fahdshariff.blogspot.com/2011/04/writing-your-own-bash-completion.html” tutorial.

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?

am I mistaken to assume the above is the entire content of that file? If so … where could I get the rest from?