How does Bash in Ubuntu know a tool's specific list of actions?

For example if I type apt-get and tab twice I only see remove, update, upgrade ...etc, but not the actions for another command or the files in the current directory.

I'm developing a command-line tool in Go and would like to provide this feature for the distros that support it.

  • 1
    I removed "ubuntu" from your title and tags since this is better as a more general question (the answer isn't specific to any distro). – goldilocks Aug 11 '14 at 18:55
  • 1
    Great question, I've often wondered this, but never took the time to look it up. – akatakritos Aug 18 '14 at 20:14

It does this using bash v4's completion features. The completion code for apt-get is provided by the bash-completion package and located at /usr/share/bash-completion/completions/apt-get. Applications that have completion and are not part of the base bash-completion package place their completion scripts in /etc/bash_completion.d.

The completions are loaded via sourcing /etc/bash_completion. Exactly where this is done will vary depending on Debian or Ubuntu versions. That in turn will source everything in /usr/share/bash-completion/completions and /etc/bash_completion.d.

  • Thanks, that explains it. So the implementation no only varies from Debian to Ubuntu, but also in each version of the same distribution? – Amged Rustom Aug 11 '14 at 17:57
  • 1
    @Amgad It works the same way on all distributions. The only differences are where the files are located, and perhaps some default settings. – Gilles 'SO- stop being evil' Aug 11 '14 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.