How is tab execution implemented for linux commands?

I noticed that sometimes commands can be completed.

eg. xm command in xen.

enter xm [space] [tab] and print out the actual parameters which are:

addlabel destroy info network-attach resume sysrq vnet-delete block-attach dmesg labels network-detach rmlabel top vnet-list block-detach domid list network-list save trigger vtpm-list block-list domname loadpolicy new sched-credit unpause cfgbootpolicy dry-run log pause sched-sedf uptime console dump-core makepolicy reboot serve vcpu-list create dumppolicy mem-max rename shutdown vcpu-pin debug-keys getlabel mem-set resources start vcpu-set delete help migrate restore suspend vnet-create 

This is a pretty spot!

How can I implement my own command completion on Linux?

+6
source share
4 answers

This is a fairly broad question, but the general idea is that you are registering something using the built-in compgen or complete . They are described in the manual . The previous section describes the general topic of programmatic termination through processing completion attempts.

For a whole ton of examples, see /etc/bash_completion , which provides everything by default that comes with bash (outside of the fully embedded stuff, such as file name completion). For more examples, see /etc/bash_completion.d ; they are automatically generated by /etc/bash_completion as a default extension method.

+7
source

This is done through the shell using the GNU Readline library in case of bash

+1
source

bash smart termination is handled by a series of scripted bash functions. On Debian, possibly Ubuntu, and possibly other Linux distributions, you can find your complete systems in /etc/bash_completion.d .

The official documentation for this mechanism is at http://www.gnu.org/software/bash/manual/bash.html#Programmable-Completion

+1
source

Source: https://habr.com/ru/post/908482/


All Articles