CLI magic: programable bash completion

May 4th, 2007 mysurface Posted in apt-cache, apt-get, Bash, complete, pipeline, sort, svn | Hits: 129422 | 11 Comments »

Ubuntu, a Linux distribution that is getting popular because it has successfully improves it’s user friendliness with simple User interface. You can install any applications with few clicks, configuration under GUI dialogs etc. People usually have the thinking of maybe one day, Ubuntu will become another new operating system like Microsoft Windows.

Never! The concept of making everything easy by using graphics user interface is wrong. Ubuntu Feisty have bundle with tons of new GUI applications to ease the job of configuration such as networking, wireless access, vpn, multimedia codec installation etc, but it also packaged with some amazing command line interface features, one of them is bash completion.

Most of us may had experienced the conventional bash completion by hitting tab twice. For example, while typing a command name halfway and tabbing two time, it will shows you a list of available commands. Let say i type

svn [tab][tab]

will list all these

svn            svnadmin       svndiff        svndumpfilter  svnlook        svnpath        svnserve       svnsync        svnversion

So what is so special about the programmable bash completion?
What if bash completion is also available on certain commands? such as apt-get, svn, help etc?

We usually need to refers back what options a command have by specified –help or -h. What if the command have auto completion on its options?

Lets look at svn,

svn c[tab][tab]
cat       checkout  ci        cleanup   co        commit    copy      cp

This shows you that you can do svn cat,svn checkout, svn ci etc.

Not only auto completion on options, seems that it is programmable, you can actually write a function and bind it with a command. Lets look at apt-get for example. Conventionally, if we wants to list all available packages with some leading characters, for example ‘can…’, we need to do this

apt-cache pkgnames can

But now you can do this,

apt-get install can[tab][tab]

Pretty cool isn’t it?

Another example, bash completion can bind with certain file format with certain file extension. Let say you have a folder name download, where you have stored all archive files such as tar.gz, .zip, .rar etc. When you do this,

tar zxvf [tab][tab]

It will list all the files with extension of .tar.gz only.

How to install?
Certain Linux distribution may have already installed, you just need to turn it on by uncomment these lines in /etc/bash.bashrc

# enable bash completion in interactive shells
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion

You may try to apt-get if you are using Debian based distro.

apt-get install bash-completion

Looking for source code, and manual installation?

I was wandering what commands has the ability of bash completion?

complete | sort -o complete.txt

Refers to the command line above, I have store all the information of what commands have bash completion into a file complete.txt. To define bash completion, we uses a bash command call ‘complete’. The complete.txt list all the complete command line. It might look a bit complicated,

complete -F _wvdial wvdial
complete -g groupdel
complete -g groupmod
complete -g newgrp
complete -j -P '%' disown
complete -j -P '%' fg
complete -j -P '%' jobs
complete -o default -F _aptitude aptitude
complete -o default -F _aspell aspell
complete -o default -F _configure_func configure
complete -o default -F _cvs cvs
complete -o default -F _dpkg_reconfigure dpkg-reconfigure
complete -o default -F _gpg gpg
complete -o default -F _iconv iconv
complete -o default -F _lftp lftp
complete -o default -F _longopt env
complete -o default -F _longopt netstat
complete -o default -F _longopt seq
complete -o default -F _longopt uname
complete -o default -F _longopt units
complete -o default -F _longopt wget
complete -o default -F _look look

You may not understand what -F -o is for at first, but it does provide hints of what commands have bash completion by looking at the last few words in each line. For examples, wvdail, groupdel … units, wget, look have bash completion.

Where do I learn how to program on bash completion for my packages?
Bash man page, or online bash references. If you just want to understand the meaning and usage of the complete’s options, read Programmable Completion Builtins.

The code is actually at /etc/bash_completion, with all the function definition.

Other bash completion articles?

  • CLI Magic: Bash complete at
  • Bash Completion – makes life easier for linux users from All About Linux.
  • Bash Completion at linux-mag.
  • [中文翻译]

    [tags]bash, auto completion, bash completion, bash programming[/tags]

    11 Responses to “CLI magic: programable bash completion”

    1. In ubuntu feisty, I realize that when I type a not-yet-installed command, the bash output this:

      $ php5
      The program 'php5' is currently not installed. You can install it by typing:
      sudo apt-get install php5-cli
      bash: php5: command not found

      I wonder what script that does the job.

    2. toydi: yes, you are right. It is command-not-found package. I gonna cover this on next post.

    3. […] Bash Completion is a powerful CLI magic for bash user, it allows users to hit tabs and have auto completions on command lines. If you have no idea what is bash completion, check out CLI magic: programmable bash completion. […]

    4. Thank you for posting this! You motivated me to seek out a newer version of bash-completion.

      I was still running a version from 2004 (which I blogged about, back then), which certainly wouldn’t have an up-to-date command list.

      The bash I use is in Cygwin, so I found a site (one of many) to download the Cygwin versions of bash-completion from:

      For the record, it was your recently follow-up entry, A Bash Completion Tip, that led me to this one, which led me to dig up my old blog entry for comparison. Then, I went looking for a new bash-completion version. :)

    5. […] CLI magic: programable bash completion 中文翻译:神奇的命令行:可编程的bash补全 […]

    6. I love your blog.. very nice colors & theme. Did you create this website yourself? Plz reply back as I’m looking to create my own blog and would like to know wheere u got this from. thanks

    7. Yah!!!!! 3rd comment nice!

    8. […] More examples […]

    9. […] The swiss army knife of bash command completion […]

    Leave a Reply