You are not logged in.

#1 2013-02-08 20:05:39

jamtat
Member
Registered: 2008-03-13
Posts: 229

separate bash histories by terminal: what's wrong with this solution?

Hi. I usually have several terminals open at any given time, one of which runs a screen session to which I re-attach/detach daily. It gets kind of annoying when, for example, I ssh in and want to  re-attach to screen, and I have to scroll back through a bunch of extraneous history that actually applies to another terminal, in order to get to the screen -r command. So I started hunting around for ways to segregate bash history by terminal. I found something that works, though is a bit kludgy--see below:

HISTFILE=~/.history/history.$$
# close any old history file by zeroing HISTFILESIZE  
HISTFILESIZE=0

With those entries pasted into .bashrc, the history of each terminal gets segregated, I believe by pid, within the ~/.history directory (NOTE: that directory needs to be created before you add those lines to your .bashrc).

I can confirm that this works and gives results pretty much along the lines I want: no more logging in to re-attach to screen and having to scroll through a lot of irrelevant lines to get to the command I need. But it is a bit kludgy, as follows.

That ~/.history directory just fills up with each opening/closing of terminals. And, of course, with each reboot (which I don't do very often), all previously-existing content is going to be irrelevant. So at the least, this method can be augmented by automating the removal of all content from that directory each time the system shuts down.

But there may be other improvements, or maybe even a more elegant way to accomplish the end for which I'm aiming. Or there may even be some unforeseen problem with this approach. Anyone care to offer critiques, improvements, or other suggestions?

Thanks,
James

Offline

#2 2013-02-08 20:51:54

jamtat
Member
Registered: 2008-03-13
Posts: 229

Re: separate bash histories by terminal: what's wrong with this solution?

Actually, I were wrong: this doesn't address the ssh/screen re-attach issue. I guess that's because each time I ssh in, a new instance of bash with a new PID gets started. Consequently, with the way my .bashrc has been modified, that history is empty. So some other solution will have to be implemented for that--like maybe dumping all ssh logins into the running screen session? Dunno. I'll have to think about it. Still, separating bash histories for terminals opened on the local machine looks useful, so I think I'll keep that.

James

Offline

#3 2013-02-08 21:01:42

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: separate bash histories by terminal: what's wrong with this solution?

Wouldn't it be simpler to just use Ctrl-r? Or write some aliases for each specific machine?


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#4 2013-02-08 21:38:13

doug piston
Member
From: Seattle
Registered: 2011-09-11
Posts: 387
Website

Re: separate bash histories by terminal: what's wrong with this solution?

From what I gather you are just trying to scroll through your history a bit quicker? If so have a look here https://wiki.archlinux.org/index.php/Readline#History
You type the beginning of your command and then 'search' (Up/Down arrows) only searches that command. The wiki explains it better then I just did. sad

Last edited by doug piston (2013-02-08 21:38:48)

Offline

#5 2013-02-08 22:59:50

jamtat
Member
Registered: 2008-03-13
Posts: 229

Re: separate bash histories by terminal: what's wrong with this solution?

I obviously stand to learn a lot about bash. I'd never heard (at least not that I recall, anyway), in the 10+ years I've been struggling to learn my way around GNU/Linux, about ctrl-r as a way of searching bash history. There's also some helpful information at that link, doug piston--thanks for pointing that out. I need to get out more or something. Now I need to figure out whether the segregation trick I learned about might be worthwhile keeping despite the more effective ways of dealing with bash history that have been pointed out here. Thinking . . .

James

Offline

#6 2013-02-09 01:24:33

pilotkeller
Member
From: Canada
Registered: 2011-12-22
Posts: 513
Website

Re: separate bash histories by terminal: what's wrong with this solution?

Although boring as a horses behind, you really should read every guide on tldp.org cover to cover.
You'll be shocked and amazed at everything you learn.

Offline

#7 2013-02-10 18:00:50

gsgleason
Member
Registered: 2012-10-08
Posts: 72

Re: separate bash histories by terminal: what's wrong with this solution?

Why not segregate by tty instead of pid?  Since there are slashes in your tty, you'd have to either replace the slash or escape it.  Here is an example of replacing the slash with an underscore.

HISTFILE=~/.history/history.$(tty|tr / _)

Offline

#8 2013-02-12 01:41:32

jv2112
Member
Registered: 2011-07-23
Posts: 160

Re: separate bash histories by terminal: what's wrong with this solution?

This may also help you keep better records by adding to your .bashrc.

PROMPT_COMMAND='history -a; history -n' # write command to history when issued as opposed to the default end of session. 
shopt -s histappend # append - not truncate

Life is pleasant. Death is peaceful. It's the transition that's troublesome. Isaac Asimov - / -

Offline

Board footer

Powered by FluxBB