Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,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 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[8,041 views]  

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 whomever posted them. This site is not responsible for what they say.
Interesting..
Authored by: Chas on Thu, Apr 4 2002 at 2:29PM PST
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 Thu, Apr 4 2002 at 2:42PM PST
Make sure you end each session by typing "exit". If you don't, then nothing gets saved...

-rob.

[ Reply to This | # ]
Probably too basic, but...
Authored by: rebug on Thu, Apr 4 2002 at 8:21PM PST
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 Thu, Apr 4 2002 at 8:55PM PST
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 Thu, Apr 4 2002 at 10:00PM PST
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: kennethmac2000 on Fri, Apr 5 2002 at 7:14PM PST
Any way to get logout to run when you close a Terminal window?

[ Reply to This | # ]
Probably too basic, but...
Authored by: serversurfer on Thu, Apr 18 2002 at 11:21AM PDT
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 | # ]
Thanks!
Authored by: kwalker on Thu, Apr 4 2002 at 10:55PM PST
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 Wed, Mar 17 2004 at 6:01AM PST
For bash add "history -a $HISTFILE" to ~/.bash_logout

[ Reply to This | # ]