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.


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.


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.


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?

  • 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

Ill add something to Gert's answer.


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


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 2 '17 at 9:03

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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.