8

I face this issue on some of Unix servers. When I open different session for same user, command history is shared by all the session. This creates issues if by mistake I press Ctrl-P or up arrow and just press Enter. On one occasion I end up running rm -rf * thankfully I was in directory where I don't have permissions to remove files.

How to have separate command history for different sessions for the same user? Most of the time I am using ksh and tcsh shells.

tshepang
  • 65,642
Hemant
  • 6,910
  • 5
  • 39
  • 42

5 Answers5

11

You can add HISTFILE=~/.hist$$ to your .profile. This should generate a unique file per session.

You will end up with a large number of .hist* so I suggest you remove them occasionally.

Gert
  • 9,994
8

From the ksh faq:

Q1. How do I get separate history files for shell?

A1. ksh uses a shared history file for all shells that use the same history file name. This means that commands entered in one window will be seen by shells in other windows. To get separate windows, the HISTFILE variable needs to be set to different name before the first history command is created.

theotherreceive
  • 2,086
  • 2
  • 16
  • 9
2

I'm assuming you are talking about simultaneous sessions; separating sessions that are after one another isn't very useful: you would never be able to use it's history anymore, because all sessions would be unique. If that were the case, you would probably be better off just disabling command history.

If we are talking about separating simultaneous session, I'd recommend you use Bash. I'm pretty sure I'm not getting the command history from two simultaneous sessions mixed with it. Bash only writes history to .bash_history at logout, so command history won't get mixed until after logout. Provided that using Bash is acceptable, would that solve your issue?

Anthon
  • 79,293
wzzrd
  • 3,720
  • thanks for your reply. I am working on HP-UX and its not have bash installed and I dont have permission to install it :(. – Hemant Aug 16 '10 at 10:51
1

Ill add something to Gert's answer.

Gert's

You can add HISTFILE=~/.hist$$ to your .profile. This should generate a unique file per session.

Mine

You can remove these files automatically by adding the following.

trap 'rm ${HISTFILE}' exit

  • 2
    Be a bit circumspect about exit traps - the shell doesn't stack them as you might hope it did - so any further setting of an exit trap will over-write all previous ones. I wrote a stacker for traps, but it nearly blew every mental fuse I had. – david collier Aug 02 '17 at 09:03
-1

You could use screen. I also made a variable that was defined by my konsole profile and gave each of sessions a different history file, only ways I can think of.

xenoterracide
  • 59,188
  • 74
  • 187
  • 252
  • I cant install screen. its not even compiling on my server. – Hemant Aug 16 '10 at 10:51
  • I'm not sure how screen would give you that capability anyway – Michael Mrozek Aug 16 '10 at 13:26
  • @Michael you can have different sessions with screen and screen has it's own history. I dunno someone told me to solve my problem with screen... I didn't like screen. lame that this got downmodded when my second answer is essentially the same as the other ones that got upmodded. @Hemant ... and I was supposed to know that? I don't use screen to do this on my system. I just know it allows for a similar effect done right. – xenoterracide Aug 16 '10 at 19:39
  • I think I know what you're describing, but it's not a screen feature, it's just a side effect of the way shells work. If you open two shells, the commands typed in one won't show up in the history of the other unless the shell specifically supports it (for example, ZSH does, but you need to explicitly enable it). It doesn't keep separate histories, they both write to the main history file, you just can't see the changes from each shell because typically shells don't check for history file changes once they've loaded – Michael Mrozek Aug 16 '10 at 20:49
  • @MichaelMrozek The korn shell has one shared history for all windows, this cannot be disabled. Using screen will not help at all. – FUZxxl Oct 21 '15 at 14:23
  • @FUZxxl Yes, the accepted answer says that, and "screen will not help at all" was the point of my comment – Michael Mrozek Oct 21 '15 at 14:33
  • I think this depends on what part of shared history the OP is trying to accomplish, though I'm not that familliar with ksh. Screen has it's own built in history and if you're just looking for terminal scrollback. My answer is sort of built around that the question may be implying an answer, rather than defining the problem. Especially because there are good answers covering other aspects. I had a similar problem and it wasn't an actual desire to have a different history file. – xenoterracide Oct 21 '15 at 14:49