Working more productively with bash 2.x/3.x

by Ian Macdonald



Overview and Introduction

bash is a shell, a.k.a. command language interpreter for modern computers. It is Bourne shell (sh) compatible and incorporates useful features from the Korn shell (ksh) and C shell (csh). It conforms to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard.

Some of bash's features are, in no particular order: editing and completion; history and command re-entry; job control; shell functions and aliases; arrays; arithmetic; ANSI C quoting; tilde expansion; brace expansion; substring capabilities; indirect variable expansion; expanded I/O capabilities; control of built-in commands; help; directory stack; POSIX mode; internationalisation; command timing.

bash is the default shell on all popular GNU/Linux distributions. For this reason, it's widely used, but rarely to anything like its full potential.

bash is, perhaps, the Linux program you use the most at work, yet how much are you really getting out of it? If you would like to enhance your productivity by reducing the amount of keystrokes you type on a given day, this document is for you.

bash Tips and Tricks

readline Tips and Tricks

The readline library is used by bash and many other programs to read a line from the terminal, allowing the user to edit the line with standard Emacs editing keys.

Programmable Completion

Introduction

A relatively new feature in bash is programmable completion, which has been available since the beta version of 2.04. Programmable completion will be familiar to you if you are a zsh user. It also exists, albeit in a much less usable form, in tcsh.

It's much easier to demonstrate programmable completion than it is to explain it, so I suggest installing one of the files below and trying it out. If you don't like it, you can always disable it for a particular command, disable it completely, or remove it from your system. It can be installed and removed very cleanly, so you shouldn't be wary of giving it a whirl.

Standard completion

bash has offered many forms of completion since its inception, including path, file, user, host and variable completion.

Programmable completion indefinitely extends the type of completion you can perform.

Download

The following files are available for download. All of them are made available under the GNU General Public License.

FileType
bash-completion-20060301.tar.gztarred and gzipped source for bash 2.05 and later, including bash 3.x
bash-completion-20060301-1.noarch.rpmBinary RPM for bash 2.05-12 and later, including bash 3.x
bash-completion-20060301-1.src.rpmSource RPM for bash 2.05-12 and later, including bash 3.x
ChangelogCVS change log of bash_completion
bash-3.2-105.i386.rpmBinary RPM of bash 3.2 patch level 17 (including documentation)
bash-3.2-105.src.rpmSource RPM of bash 3.2 patch level 17
bash-3.1-108.i386.rpmBinary RPM of bash 3.1 patch level 17 (including documentation)
bash-3.1-108.src.rpmSource RPM of bash 3.1 patch level 17
bash-3.0-113.i386.rpmBinary RPM of bash 3.0 patch level 16 (including documentation)
bash-3.0-113.src.rpmSource RPM of bash 3.0 patch level 16
bash-2.05b-63.i386.rpmBinary RPM of bash 2.05b (including documentation)
bash-2.05b-63.src.rpmSource RPM of bash 2.05b
bash-2.05a-service_completion.patchPatch (release 20020207) to bash 2.05a to add service completion
bash-2.05a-55.i386.rpmBinary RPM of bash 2.05a with service completion patch
bash-doc-2.05a-55.i386.rpmBinary RPM of bash 2.05a documentation
bash-2.05a-55.src.rpmSource RPM of bash 2.05a with service completion patch
bash-2.05-group_completion.patchPatch to bash 2.05 to add group completion
bash-2.05-service_completion.patchPatch to bash 2.05 to add service completion (requires prior application of the group completion patch)
bash-2.05-51.i386.rpmBinary RPM of bash 2.05 with above patches applied
bash-doc-2.05-51.i386.rpmBinary RPM of bash 2.05 documentation
bash-2.05-51.src.rpmSource RPM of bash 2.05 with above patches applied

Installation

If you wish to install the binary RPM, execute the following command as root:

# rpm -Uvh bash-completion-xxxxxxxx-x.noarch.rpm

Afterwards, you will find you have a new file on your system, /etc/bash_completion.

Installation of the RPM used to modify /etc/bashrc to source /etc/bash_completion, but this is inherently dangerous, so as of the 20030505 release, /etc/bashrc is no longer touched (except to clean up any additions made by previous versions of the RPM.

Instead, a stub script will be installed as /etc/profile.d/bash_completion.sh, which will call /etc/bash_completion if a suitable version of bash is detected.

If you are installing the source file, either gunzip or bunzip2 it, put it somewhere on your system and source it from /etc/profile.d/bash_completion.sh, /etc/bashrc or ~/.bashrc. If you are putting it somewhere other than /etc/bash_completion, you will need to edit the top of the file to make $BASH_COMPLETION point to the correct location.

The line you need to adjust is this one:

BASH_COMPLETION="${BASH_COMPLETION:-/etc/bash_completion}"

If you don't see the above line, you are probably using version 20060301 or earlier. Please upgrade to a more recent version.

Getting started

The first thing to do is source the completion file. As mentioned above, the best way to do this is by having your shell source it at launch time, but that doesn't do you any good in the shells that you already have open. To try out completion in an already open shell, execute the following command:

. /etc/bash_completion

You also need to do this if you have elected to install the software from an RPM package.

Once sourced, you can obtain a complete listing of all commands that have associated completions with complete -p. Additionally, declare -f will show you the code of all shell functions, including those to which the completions are bound.

To get acquainted with programmable completion, it's probably best to just type some of these commands and then hit <Tab> at various positions along the command line. Many commands complete on different things, depending on the position and context of the token on the command line. This screenshot may help you better envisage how completion works in practice.

If you're a sysadmin type, you'll find ssh completion particularly useful, since this completes hostnames based on the contents of your known_hosts files. Also, if you NFS mount a lot of hosts, try mount hostname:<Tab>.

Software engineers will perhaps find p4 and cvs completion useful.

Implementation

The group completion feature was added in bash 2.05a and is used by the bash completion code, so if you are using bash 2.05, you will either need to apply the source patch offered above, or download a prepatched binary. A better idea is to upgrade to at least 2.05b.

If you are using bash 2.04, you are not only missing group and service completion, but also the -o option to complete. This is used extensively by the completion code, so I would again recommend upgrading to bash 2.05b or later.

bash 3.1 is now available. Initial reports suggest that there are some minor incompatibilities between this version and the 20050721 release of the bash completion code, which many people are still using. Please ensure that you upgrade to the 20060301 release, which is believed to fix these issues.

If you're using bash 3.0 or later, I strongly recommend that you apply all of the official GNU patches for your version.

Bug fixes and enhancements

If you find any bugs or wish to make any enhancements, I would be happy to receive your patches to the code. In particular, gcc completion and exhaustive treatment of cvs would be nice. Another strong desire is to make much of the code portable without slowing it down. Patches in all of these areas are welcome.

New releases

If you want to be informed as soon as a new version of the code is released, please subscribe to release announcements.

Contacting me

If you can't submit a patch, but would like to make a suggestion, please do get in touch with me. If your suggestion would benefit many people, there's a good chance I'll implement it.

If you have an opinion of this code and you're a registered Freshmeat user, please take a moment to rate it.