Working more productively with bash 2.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 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


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.


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

bash-completion-20020507.tar.gztarred and gzipped source for bash 2.05 and later
ChangelogCVS change log of bash_completion
bash-completion-20020507-1.noarch.rpmBinary RPM for bash 2.05-12 and later
bash-completion-20020507-1.src.rpmSource RPM for bash 2.05-12 and later
bash-2.05a-service_completion.patchPatch (release 20020207) to bash 2.05a to add service completion
bash-2.05a-54.i386.rpmBinary RPM of bash 2.05a with above patch applied
bash-doc-2.05a-54.i386.rpmBinary RPM of bash 2.05a documentation
bash-2.05a-54.src.rpmSource RPM of bash 2.05a with above patch applied
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


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. Assuming you didn't add the --noscripts switch to rpm when you installed, the post-installation of the RPM will have modified your /etc/bashrc to contain the following chunk of code:

# START bash completion -- do not remove this line
bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
if [ "$PS1" ] && [ $bmajor -eq 2 ] && [ $bminor '>' 04 ] \
   && [ -f /etc/bash_completion ]; then # interactive shell
        # Source completion code
        . /etc/bash_completion
unset bash bmajor bminor
# END bash completion -- do not remove this line

If you are installing the source file, either gunzip or bunzip2 it, put it somewhere on your system and source it from either /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:

[ -z "$BASH_COMPLETION" ] && declare -r BASH_COMPLETION=/etc/bash_completion

Getting started

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.


The group completion feature was added in bash 2.05a. This feature is used by my 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.

If you are using bash 2.04, you are not only missing group completion, but also the -o flag to complete. This is used extensively in my completion code, so I recommend upgrading to bash 2.05a in this case.

Although I offer a source patch to both bash 2.05 and 2.05a to add the feature of service completion, this is currently not used by the bash completion code. It's thus unnecessary to apply this patch unless you want to want to write your own completion definitions that use it.

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.

Home | Wedding | About Ian | Mailing lists | PCW | signature | bash | Ruby | Contact Ian | Contact Sarah

Last modified: Tuesday, 7 May 2002 at 01:28 PDT