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
Try this:
$ export CDPATH=.:~:~/docs:~/src:~/src/ops/docs:/mnt:/usr/src/redhat:/usr/src/redhat/RPMS:/usr/src:/usr/lib:/usr/local:/software:/software/redhat
Using this, cd i386 would likely take you to /usr/src/redhat/RPMS/i386 on a Red Hat Linux system. Make sure that you do include . in the list or you'll find that you can't change to directories relative to your current one without prefixing them with ./
Try this:
$ export HISTIGNORE="&:ls:ls *:mutt:[bf]g:exit"
Using this, consecutive duplicate commands, invocations of ls, executions of the mutt mail client without any additional parameters, plus calls to the bg, fg and exit built-ins will not be appended to the history list.
Try adding the following to your ~/.bash_profile to be notified when any new mail is deposited in any mailbox under ~/Mail.
MAILPATH=/var/spool/mail/$USER
for i in ~/Mail/[^.]*
do
MAILPATH=$MAILPATH:$i'?You have new mail in your ${_##*/} folder'
done
export MAILPATH
unset i
If you use mutt and many of those folders don't receive automatically filtered mail, you may prefer to have bash alert you only when new e-mail arrives in a folder that you also track in mutt.
In that case, try something like the following in your ~/.bash_profile:
MAILPATH=`perl -ne 's/^mailboxes // && chomp && s/~/$ENV{HOME}/g && s/ |$/?You have new mail in your \\$\\{_##*\/} folder:/g && chop && print && exit' < ~/.muttrc`
This setting is useful in root's environment to reduce the potential security risk of someone forgetting to log out as the superuser.
You can set each of the options below with shopt -s <option>.
Here's an example. Say, you wanted to install all RPMs in a given directory,
except those built for the
rpm -Uvh /usr/src/RPMS/!(*noarch*)
These expressions can be nested, too, so if you wanted a directory listing
of all non PDF and PostScript files in the current directory, you might
do this:
ls -lad !(*.p@(df|s))
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.
If you have this in your /etc/inputrc or ~/.inputrc, you will no longer have to hit the <Tab> key twice to produce a list of all possible completions. A single <Tab> will suffice. This setting is highly recommended.
Adding this to your /etc/inputrc or ~/.inputrc will result in a character being appended to any file-names returned by completion, in much the same way as ls -F works.
If you're a fan of vi as opposed to Emacs, you might prefer to operate bash in vi editing mode. Being a GNU program, bash uses Emacs bindings unless you specify otherwise.
Set the following in your /etc/inputrc or ~/.inputrc:
set editing-mode vi
set keymap vi
and this in your /etc/bashrc or ~/.bashrc:
set -o vi
Some people prefer the non-incremental style of history completion, as opposed to the incremental style offered by C-r and C-s. This is the style of history completion offered by csh.
bash offers bindings for this, but they are unbound by default.
Set the following in your /etc/inputrc or ~/.inputrc:
"\ep": history-search-backward
"\en": history-search-forward
Henceforth, ESC-p and ESC-n (or M-p and M-n) will give you non-incremental history completion backwards and forwards, respectively.
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.
bash has offered many forms of completion since its inception, including
path, file, user, host and variable completion.
This type of completion occurs on the first token of the command line, allowing
you to complete on executable files. Together with file-name completion,
it is the most known and used type.
This allows you to complete on file and directory names
at the second and subsequent token position on the command line.
This allows you to complete on user names by prefixing the
token with a ~ (tilde).
This allows you to complete on host names by prefixing the
token with a @.
This allows you to complete on variable names by prefixing the
token with a $.
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.
Standard completion
Download
File | Type |
---|---|
bash-completion-20060301.tar.gz | tarred and gzipped source for bash 2.05 and later, including bash 3.x |
bash-completion-20060301-1.noarch.rpm | Binary RPM for bash 2.05-12 and later, including bash 3.x |
bash-completion-20060301-1.src.rpm | Source RPM for bash 2.05-12 and later, including bash 3.x |
Changelog | CVS change log of bash_completion |
bash-3.2-105.i386.rpm | Binary RPM of bash 3.2 patch level 17 (including documentation) |
bash-3.2-105.src.rpm | Source RPM of bash 3.2 patch level 17 |
bash-3.1-108.i386.rpm | Binary RPM of bash 3.1 patch level 17 (including documentation) |
bash-3.1-108.src.rpm | Source RPM of bash 3.1 patch level 17 |
bash-3.0-113.i386.rpm | Binary RPM of bash 3.0 patch level 16 (including documentation) |
bash-3.0-113.src.rpm | Source RPM of bash 3.0 patch level 16 |
bash-2.05b-63.i386.rpm | Binary RPM of bash 2.05b (including documentation) |
bash-2.05b-63.src.rpm | Source RPM of bash 2.05b |
bash-2.05a-service_completion.patch | Patch (release 20020207) to bash 2.05a to add service completion |
bash-2.05a-55.i386.rpm | Binary RPM of bash 2.05a with service completion patch |
bash-doc-2.05a-55.i386.rpm | Binary RPM of bash 2.05a documentation |
bash-2.05a-55.src.rpm | Source RPM of bash 2.05a with service completion patch |
bash-2.05-group_completion.patch | Patch to bash 2.05 to add group completion |
bash-2.05-service_completion.patch | Patch to bash 2.05 to add service completion (requires prior application of the group completion patch) |
bash-2.05-51.i386.rpm | Binary RPM of bash 2.05 with above patches applied |
bash-doc-2.05-51.i386.rpm | Binary RPM of bash 2.05 documentation |
bash-2.05-51.src.rpm | Source 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.
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.
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:
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.
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.
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.
If you want to be informed as soon as a new version of the code is released,
please subscribe to
release announcements.
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.
Getting started
. /etc/bash_completion
Implementation
Bug fixes and enhancements
New releases
Contacting me