Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Linux Timesaving Techniques For Dummies.pdf
15.98 Mб

52 Technique 9: Making History (Work for You)

4. If you want the most recent ssh command (ssh versailles), just press Enter. If you want an earlier ssh command (ssh louvre), press Ctrl-R to tell bash to keep looking.

It’s easy to make mistakes while you’re getting familiar with the history command. You can save a lot of time if you ask bash to show you each command after expansion but before the command is executed. The histverify shell option does the trick; just add shopt -s histverify to your ~/.bashrc file.

When you’re comfortable with the history feature, you may want to turn off histverify. You can still press Esc and then Ctrl-E to preview your command line after expansion.

Customizing the History List

bash gives you a lot of control over the history list. To customize the history list for how you work, you can modify the defaults and filter out commands that just get in your way.

Adjusting key default settings

Here are the defaults that you’ll likely want to modify:

To adjust the number of commands that bash remembers, use $HISTSIZE.

To set the number of commands that bash remembers from session to session, use $HISTFILESIZE.

To change the location of the saved history file from the default (~/.bashrc), modify the $HISTFILE environment variable.

See the previous technique (Technique 8) to find out how to make your preferences permanent so they take effect every time you log in.

Filtering the history list

You can also filter out certain commands from the history list. After you’ve used the history feature awhile, you’ll probably notice that some commands really don’t belong in the history list. Here are a few examples:

It’s redundant to maintain the history command itself in the history list.

It’s unnecessary to record exit commands. (The exit command will log you out of the shell.)

It’s a little dangerous to keep rm commands (or other data-destroying commands) in your history list because you might recall them by accident.

You filter out those nasty (or just plain annoying) commands with the $HISTIGNORE variable. Set $HISTIGNORE to a colon-separated list of patterns to exclude from the history list. To filter out the commands we just mentioned, use this:

$ export HISTIGNORE=”history:exit:rm *”

You may also want to filter out repeated commands. To do so, include the magic character & in $HISTIGNORE:

$ export HISTIGNORE=”&:history:exit:rm *”

Occasionally, you’ll type in a command that you know you don’t want stored in the history list (maybe you’re restoring files from an archive and you don’t want to risk doing it again later by accident). To exclude from the history list any command that starts with a space or tab, add the pattern [ \t]*

to $HISTIGNORE (be sure to include the space between [ and \):

$ export HISTIGNORE=

”[ \t]*:&:history:exit:rm *”

Соседние файлы в предмете Операционные системы