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
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.
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 `echo ~/Mail/[^.]*` do MAILPATH=$MAILPATH:$i 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:
export `perl -ne 's/^mailboxes /MAILPATH=/ && tr/ /:/ && 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
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-20020507.tar.gz | tarred and gzipped source for bash 2.05 and later |
Changelog | CVS change log of bash_completion |
bash-completion-20020507-1.noarch.rpm | Binary RPM for bash 2.05-12 and later |
bash-completion-20020507-1.src.rpm | Source RPM for bash 2.05-12 and later |
bash-2.05a-service_completion.patch | Patch (release 20020207) to bash 2.05a to add service completion |
bash-2.05a-54.i386.rpm | Binary RPM of bash 2.05a with above patch applied |
bash-doc-2.05a-54.i386.rpm | Binary RPM of bash 2.05a documentation |
bash-2.05a-54.src.rpm | Source RPM of bash 2.05a with above patch applied |
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. 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 fi 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
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.
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.
Last modified: Tuesday, 7 May 2002 at 01:28 PDT
Implementation
Bug fixes and enhancements
New releases
Contacting me