LXD container name autocomplete with bash script


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.
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.