Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Save command history from multiple shells UNIX
So I was setting up my multiple windows in the Terminal according to this article on O'Reilly's site (upon searching Mac OS X Hints, I found similar tips 1 & 2). Everything was looking and acting great. I created three windows that would open up when I launched Terminal (one for man pages, one for working, and one for misc commands).

Now as long as each window remained open, that window had its individual command history. However, when I logged out of all my windows individually and either restarted Terminal or opened a new terminal window I found that the majority of the commands I had used in all of the windows were NOT saved. As I did some testing I found that only the command history of the last window I logged out of was saved to .tcsh_history. After some reading of the man page for tcsh (shudder), I came up this little nugget of information:
history -S|-L|-M [filename] (+)

With -S, the second form saves the history list to filename. If the first word of the savehist shell variable is set to a number, at most that many lines are saved. If the second word of savehist is set to 'merge', the history list is merged with the existing history file instead of replacing it (if there is one) and sorted by time stamp (+). Merging is intended for an environment like the X Window System with several shells in simultaneous use. Currently it succeeds only when the shells quit nicely one after another.
So off to my .tcshrc file where I entered set savehist = (50 merge). Voila! Now the last 50 commands from all the windows are saved.
  • Currently 3.00 / 5
  You rated: 3 / 5 (4 votes cast)

Save command history from multiple shells | 9 comments | Create New Account
Click here to return to the 'Save command history from multiple shells' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Authored by: Chas on Apr 04, '02 02:29:26PM

This is very interesting because I've never been able to get the Terminal command history to work, not on any MacOS X install I've ever done. I cannot get ANY buffers saved between sessions. Apparently the default config does save a buffer from the very first time you run Terminal, I can up-arrow back and see that first session's commands, if I relaunch Term or reboot, no further commands are stored in the buffer. Any suggestions on how to fix this?

[ Reply to This | # ]
Probably too basic, but...
Authored by: robg on Apr 04, '02 02:42:15PM

Make sure you end each session by typing "exit". If you don't, then nothing gets saved...


[ Reply to This | # ]
Probably too basic, but...
Authored by: rebug on Apr 04, '02 08:21:59PM

control d works, too.

You might check the perms on ~/.tcsh_history. It would be odd (and scary) if they were wrong, but stranger things have happened.

[ Reply to This | # ]
Probably too basic, but...
Authored by: nicksay on Apr 04, '02 08:55:40PM
just checking, but what would those permissions be? currently, mine are:
-rw-------   1 nicksay  unknown
since i have the same problem as the thread starter, i'm going to try this hint right now! nicksay

[ Reply to This | # ]
Probably too basic, but...
Authored by: rebug on Apr 04, '02 10:00:32PM
josh@muffin: ls -l .tcsh_history 
-rw-------  1 josh  staff  171 Apr  2 08:07 .tcsh_history

That's what I get, and mine works fine. Looks like permissions aren't your issue.

[ Reply to This | # ]
Probably too basic, but...
Authored by: Anonymous on Apr 05, '02 07:14:30PM

Any way to get logout to run when you close a Terminal window?

[ Reply to This | # ]
Probably too basic, but...
Authored by: serversurfer on Apr 18, '02 11:21:37AM

You have to use 'exit' or '^d'. Cmd-W just closes the window, and terminates any running commands. (Like the shell itself, for example.)

[ Reply to This | # ]
Authored by: kwalker on Apr 04, '02 10:55:36PM

I've been meaning to sort this out. Thanks for the info.

To summarize: "set savehist = (500 merge)" in .tcshrc, plus ending terminal sessions with ^D or 'exit', does the trick.

[ Reply to This | # ]
bash history merge
Authored by: kmue on Mar 17, '04 06:01:48AM

For bash add "history -a $HISTFILE" to ~/.bash_logout

[ Reply to This | # ]