In the May 2002 Power Tools column (http://www.linux-mag.com/2002-05/power_01.html), we looked at one of the most compelling features of zsh: its ability to complete often-used command-line arguments and switches. If you were tempted to drop bash in favor of zsh, don't switch just yet -- bash still has a few tricks up its sleeve.
In the May 2002 Power Tools column (http://www.linux-mag.com/2002-05/power_01.html), we looked at one of the most compelling features of zsh: its ability to complete often-used command-line arguments and switches. If you were tempted to drop bash in favor of zsh, don’t switch just yet — bash still has a few tricks up its sleeve.
bash, it turns out, is just as capable as zsh and every other shell out there. The big difference is that bash doesn’t come with all the smarts built-in. Instead, bash has hooks, and leaves the real programming work (or “fun”) in your hands (this is introduced on Ian Macdonald’s “Working More Productively with bash” page at http://www.caliban.org/bash/index.shtml#completion).
The bash completion project provides a large set of rules (i.e., bash shell functions) to determine the right way to “complete” various command lines.
For example, when you type ssh and hit the TAB key, bash, with the help of rules, reads and parses your ~/.ssh/known_hosts file and presents a list of all the hosts you’ve logged onto in the past.
Out of the Box
The bash completion definitions (in /etc/bash_completion) are available (but not configured) by default if you’re using Debian. Users of RedHat and other distributions need to download and install the appropriate RPM file. See Figure One for instructions on downloading and installing the RPM.
The RPM installation process automatically modifies /etc/ bashrc to read /etc/bash_completion when bash starts. Debian users will need to edit /etc/bash.bashrc and uncomment the three bash completion lines because it is not enabled by default. The relevant lines are:
if [ "$PS1" -a -f /etc/bash_completion ]; then
Let the Fun Begin
To get an idea how helpful bash‘s completion feature is, try the ssh completion (shown in Figure Two). Type ssh and after hitting the TAB key, bash presents a list of all the known hosts.
Figure Two: Show the list of known hosts
$ ssh [TAB]
If that’s not handy enough, what if you forget the command-line arguments used to configure MySQL so that it can be compiled? bash completion helps there, too. See the sample output in Figure Three.
Figure Three: MySQL configuration options
$ ./configure -with[TAB]
This is just the tip of the iceberg. There’s a ton of commands that bash completion understands, including the bz commands (bzip2, bunzip, bzdiff, etc), mp3s (via mpg123), chown, chgrp, mount, rmmod, insmod, man, kill, find, ifconfig, make, jar, mysqladmin, rpm, and many, many others.
Have an idea for a project we should feature? Drop a note to firstname.lastname@example.org and let us know.