Полезная информация

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 11.10 Check Your History First with :p Chapter 11
The Lessons of History
Next: 11.12 Pass History to Another Shell
 

11.11 Picking Up Where You Left Off

If you want your command history to be remembered even when you log out, set the C shell's savehist shell variable (6.8) to the number of lines of history you want saved. The Korn shell and bash save history automatically; you don't need to set a variable. (If you want to change the number of lines saved by bash, set its HISTFILESIZE environment variable. In the Korn shell, the HISTSIZE variable sets the number of commands available to be recalled in your current shell as well the number saved for other shells.)

When you log out, the specified number of lines from the csh history list will be saved in a file called .history in your home directory. bash and ksh use the filename given in the HISTFILE environment variable; by default, bash calls the file .bash_history and ksh uses .sh_history.

On modern windowing systems, this isn't as trivial as it sounds. On an old-style terminal, people usually started only one main shell, so they could set the history-saving variable in their .login or .profile file and have it apply to their login shell.

However, under window systems like X or networked filesystems that share your home directory between several hosts, you have multiple shells saving into the same history file. The sections below give some possible fixes.

11.11.1 bash and ksh

Here's the basic way to give a separate history file to each Korn shell or bash: Customize your setup file (2.2) to set a different HISTFILE on each host or each window. Use names like $HOME/.sh_history.windown or ~/.bash_history.hostname to match each file to its window or host. If your setup is always the same each time you log in, that should give each window and/or host the same history it had on the last invocation. (There are related tips in articles 2.12 and 2.13.)

If you open random windows, though, you'll have a harder time automatically matching a history file to a shell the next time you log in. Cook up your own scheme.

The simplest fix is to use $$ (8.19) -which will probably expand differently in almost every shell you ever start - as a unique part of the filename. Here are two possibilities:

HISTFILE=/tmp/sh_hist.$$
HISTFILE=$HOME/.sh_hist.$$

The first example uses the system's temporary-file directory; article 21.3 explains and shows one way to clean up. If your system's /tmp is cleaned out often, you may be able to leave your history files there and let the system remove them; ask the administrator. Note that the history file may be world-readable (22.2) if your umask (22.4) isn't set to protect your files. If that matters to you, you could make the temporary files in your home directory (or some protected directory), as in the second example shown earlier. Article 3.4 shows a way to delete those files.

Two more bits of trivia:

11.11.2 C Shell

The C shell has only one possible filename for its automatic history file: .history. If you set the C shell variable savehist in each of your windows (e.g., by setting it in your .cshrc), they will all try to write .history at once, leading to trouble. And even if that weren't true, you get the history from every window or host, which might not be what you want.

Of course, you could set savehist manually in a single window when you thought you might be doing work you might want to pick up later. But there is another way: use the C shell's command history -h (which prints the history list without leading numbers, so it can be read back in later) and redirect the output to a file. Then use source -h to read it back into your history list when you log in.

Do you want to automate this? First, you'll need to choose a system of filenames, like ~/.history.windown or ~/.history.hostname, to match each file to its window or host. If each of your C shells is a login shell\** (2.8), [1] you can run history -h from your .logout file and source -h from your .login file. For non-login shells, automation is tougher - try this:

[1] In the X Window System (1.31), set the xterm -ls option to force a login shell.

If you choose to run history -h and source -h by hand occasionally, they will allow you the kind of control you need to write a script (11.12) that saves and restores only what you want.

- JP, TOR


Previous: 11.10 Check Your History First with :p UNIX Power ToolsNext: 11.12 Pass History to Another Shell
11.10 Check Your History First with :p Book Index11.12 Pass History to Another Shell

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System