dcsimg

Bash Completion

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.

http://freshmeat.net/projects/bashcompletion

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.

Hidden Secrets

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.




Figure One: Installing the bash completion RPM


$ wget http://www.caliban.org/files/redhat/RPMS/
noarch/bash-completion-20020727-1.noarch.rpm
$ rpm -i bash-completion-20020727-1.noarch.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
. /etc/bash_completion
fi

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]
216.145.52.217 dsl.zawodny.com
216.145.52.227 family.zawodny.com
216.145.54.70 litterbox.zawodny.com
217.12.5.44 morbid.corp
63.174.200.1 museful.corp
64.58.78.223 regex.info

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]
–with-PACKAGE –with-openssl
–with-berkeley-db –with-other-libc=
–with-berkeley-db-includes= –with-pstack
–with-berkeley-db-libs= –with-pthread
–with-charset= –with-raid
–with-client-ldflags –with-server-suffix
–with-comment –with-tcp-port=
–with-embedded-server –with-unix-socket-path=
–with-extra-charsets= –with-vio
–with-gnu-ld –without-PACKAGE
–with-lib-ccflags –without-bench
–with-libwrap –without-debug
–with-low-memory –without-docs
–with-mit-threads –without-extra-tools
–with-mysqld-ldflags –without-innodb
–with-mysqld-user= –without-isam
–with-mysqlfs –without-query-cache
–with-named-curses-libs= –without-readline
–with-named-thread-libs= –without-server
–with-named-z-libs=

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 potm@linux-mag.com and let us know.

Fatal error: Call to undefined function aa_author_bios() in /opt/apache/dms/b2b/linux-mag.com/site/www/htdocs/wp-content/themes/linuxmag/single.php on line 62