bash 的一些小 tips

bash 的一些小 tips

原文:http://www.caliban.org/bash/index.shtml

下面是一些节选:

bash Tips and Tricks

shell variables
$CDPATH
This is a little known and very underrated shell variable. CDPATH does for the cd built-in what PATH does for executables. By setting this wisely, you can cut down on the number of key-strokes you enter per day.

Try this:
[code:1]$ export CDPATH=.:~:~/docs:~/src:~/src/ops/docs:/mnt:/usr/src/redhat:/usr/src/redhat/RPMS:/usr/src:/usr/lib:/usr/local:/software:/software/redhat
[/code:1]
Using this, cd i386 would likely take you to /usr/src/redhat/RPMS/i386 on a Red Hat Linux system.

$HISTIGNORE
Set this to to avoid having consecutive duplicate commands and other not so useful information appended to the history list. This will cut down on hitting the up arrow endlessly to get to the command before the one you just entered twenty times. It will also avoid filling a large percentage of your history list with useless commands.

Try this:
[code:1]$ export HISTIGNORE="&:ls:ls *:mutt:[bf]g:exit"
[/code:1]
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.

$MAILPATH
bash will warn you of new mail in any folder appended to MAILPATH. This is very handy if you use a tool like procmail to presort your e-mail into folders.

Try adding the following to your ~/.bash_profile to be notified when any new mail is deposited in any mailbox under ~/Mail.
[code:1]
        MAILPATH=/var/spool/mail/$USER
        for i in `echo ~/Mail/[^.]*`
        do
                MAILPATH=$MAILPATH:$i'?You have new mail in your ${_##*/} folder'
        done
        export MAILPATH
        unset i
[/code:1]        

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:
[code:1]
        MAILPATH=`perl -ne 's/^mailboxes // && chomp && s/~/$ENV{HOME}/g && s/ |$/?You have new mail in your \\$\\{_##*\/} folder:/g && chop && print && exit' < ~/.muttrc`
[/code:1]        
$TMOUT
If you set this to a value greater than zero, bash will terminate after this number of seconds have elapsed if no input arrives.

This setting is useful in root's environment to reduce the potential security risk of someone forgetting to log out as the superuser.

set options
ignoreeof
Ordinarily, issuing Ctrl-D at the prompt will log you out of an interactive shell. This can be annoying if you regularly need to type Ctrl-D in other situations, for example, when trying to disconnect from a Telnet session. In such a situation, hitting Ctrl-D once too often will close your shell, which can be very frustrating. This option disables the use of Ctrl-D to exit the shell.
shopt options

You can set each of the options below with shopt -s <option>.

cdspell
This will correct minor spelling errors in a cd command, so that instances of transposed characters, missing characters and extra characters are corrected without the need for retyping.
cmdhist
This is very much a matter of taste. Defining this will cause multi-line commands to be appended to your bash history as a single line command. This makes for easy command editing.
dotglob
This one allows files beginning with a dot ('.') to be returned in the results of path-name expansion.
extglob
This will give you ksh-88 egrep-style extended pattern matching or, in other words, turbo-charged pattern matching within bash. The available operators are:
[code:1]
?(pattern-list)
Matches zero or one occurrence of the given patterns
*(pattern-list)
Matches zero or more occurrences of the given patterns
+(pattern-list)
Matches one or more occurrences of the given patterns
@(pattern-list)
Matches exactly one of the given patterns
!(pattern-list)
Matches anything except one of the given patterns
[/code:1]
Here's an example. Say, you wanted to install all RPMs in a given directory, except those built for the noarch architecture. You might use something like this:
[code:1]
rpm -Uvh /usr/src/RPMS/!(*noarch*)
[/code:1]
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:
[code:1]
ls -lad !(*.p?(df|s)) [/code:1]
好东西,绝对好东西,我又学到几招非常便利的招式,现在就去实践