Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers

up vote 6 down vote accepted

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.

share|improve this answer
add comment

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?

share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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