[Cialug] How to add multiple instances of bash to your bash history

Tim Wilson tim_linux at wilson-home.com
Thu Mar 20 17:23:18 CDT 2014


That was quick. :)  Ok, I'm going to include the sub-shell stuff too.  If
you're not interested in that, you can take that out.  A sub-shell is
usually a one-off type of thing.  So I don't want that history cluttering
up my main history.  Plus, I got used to that feature on an OS years ago,
and I've been able to duplicate that feature in ksh/bash.  It has the side
effect of letting you know that if you press ^D, you're going to be logging
out.  I'm sure that's happened to everyone at some point.  The
.bash_profile is the same.  In fact, you could get away with just setting
NEWWINDOW, but that requires some additional work in .bashrc since my_tty
is defined in the profile, so I'd leave it alone.  Here is my .bashrc:

# The next line adds in ${_sh+$_sh.} so we know what shell level we're at.
If there isn't a number, then we're at the main shell, not a sub-shell.
export PS1='\[\e]0;\w\a\]\n${_sh:+$_sh.}\[\e[32m\]\u@\h
\[\e[33m\]\w\[\e[0m\]\n\$ '

if [[ ${NEWWINDOW:-0} -ne 0 && $my_tty != "not a tty" ]]; then
    export THISWIN=1
    typeset -i THISWIN
    while [ -f $HOME/.windows/win$THISWIN ]; do
        THISWIN=$THISWIN+1
    done
    touch $HOME/.windows/win$THISWIN
    trap "rm $HOME/.windows/win$THISWIN" 0
    export HISTFILE=$HOME/.histories/.bash_hist.$THISWIN.0
    export HISTSIZE=512
    unset _sh
    unset NEWWINDOW
else
    let _sh=${_sh:-0}+1
    export _sh
    export HISTFILE=$HOME/.histories/.bash_hist.$THISWIN.$_sh
fi

There have been rare occasions where I have more win* files in the .windows
directory than I should.  This is usually caused by system hang, power
outage, someone killing processes, etc.  In that case, I remove every file
in .windows, log out, and log back in to reset everything.

Note, I wrote this a long time ago, so some of the code might not be the
best way to do it.  But, it works for me, so I've pretty much left it alone.

Oh, and remember to mkdir .histories and .windows!



On Thu, Mar 20, 2014 at 4:10 PM, Jeff Chapin <chapinjeff at gmail.com> wrote:

> /me raises hand
>
> I'm interested.
>
>
> On Thu, Mar 20, 2014 at 3:09 PM, Tim Wilson <tim_linux at wilson-home.com
> >wrote:
>
> > I chose a different way of doing things.  I tend to do specific things in
> > specific windows.  Compile in one, look at source in another, run an
> > application in yet another.  For me, having co-mingled histories just
> > wouldn't work.  So I have it set up so that every window I open in X
> starts
> > a login shell, which runs my .bash_profile.  In .bash_profile, I have the
> > following:
> > NEWWINDOW=1
> > LOGTTY=${LOGTTY:=`tty`}
> > my_tty=${LOGTTY#/dev/pty}
> >
> > Then in my .bashrc:
> > if [[ ${NEWWINDOW:-0} -ne 0 && $my_tty != "not a tty" ]]; then
> >     export THISWIN=$my_tty
> >     typeset -i THISWIN
> >     export HISTFILE=$HOME/.histories/.bash_hist.$THISWIN.0
> >     export HISTSIZE=512
> >     unset NEWWINDOW
> > fi
> >
> > This way, when I open that window again, I have the same history I did
> > before.  I take things a bit further by having a history per shell.  That
> > is, if I shell out of vi, I have a different history file.  That gets a
> bit
> > convoluted, so I'm not showing that here.  Also, for larger machines
> with a
> > lot of users, the ttys aren't going to be 1,2,3, etc.  Instead, I employ
> a
> > scheme of touch'ing a file in a directory to keep track of the number of
> > windows I've opened.  When I close a window, a trap call is executed to
> > remove the file.  I iterate through the files to see if there's an
> > opening.  So if I open 3 windows, close the 2nd one, and re-open it, I
> get
> > the same history, even though the tty could be way different.  If anyone
> is
> > interested in that, I can include that too.
> >
> >
> >
> > On Wed, Mar 19, 2014 at 11:55 PM, Adam Hill <adam at diginc.us> wrote:
> >
> > > Here is my standard history setup from ~/.bashrc
> > >
> > > export HISTSIZE=100000
> > > export HISTFILESIZE=50000
> > > export HISTCONTROL=ignoredups
> > > export PROMPT_COMMAND="history -a; history -n; $PROMPT_COMMAND"
> > >
> > > the -a submits your recent commands so other sessions see them, the -n
> > > pulls the hitsory back into your session so you can see your other
> > > sessions' history.
> > >
> > >               -a     Append  the  ''new'' history lines (history lines
> > > entered
> > >                      since the beginning of the current bash session)
>  to
> > >  the
> > >                      history file.
> > >               -n     Read  the history lines not already read from the
> > > history
> > >                      file into the current  history  list.   These  are
> > >  lines
> > >                      appended  to  the history file since the beginning
> > of
> > > the
> > >                      current bash session.
> > >
> > > You still usually have to press return once to see your other session's
> > > histories but it works great besides that little quirk
> > >
> > >
> > >
> > >
> > > On Wed, Mar 19, 2014 at 8:35 PM, Andrew Denner <linux-list at upeke.com>
> > > wrote:
> > >
> > > > As was discussed at the meeting tonight, by default bash only writes
> > the
> > > > last bash session running to the history blowing away any other
> > > instances.
> > > > Per
> > > >
> > >
> >
> http://www.aloop.org/2012/01/19/flush-commands-to-bash-history-immediately/
> > > >
> > > > If you add: "export PROMPT_COMMAND='history -a'" To your .bashrc file
> > it
> > > > will append each command immediately to your bash history file
> > > >
> > > > On Tuesday, March 18, 2014, Barry Von Ahsen <vonahsen at gmail.com>
> > wrote:
> > > >
> > > > > screen vs. tmux vs. byobu
> > > > >
> > > > > 7pm at Alliance
> > > > >
> > > > > http://www.cialug.org/?page_id=7&event_id=1030
> > > > > _______________________________________________
> > > > > Cialug mailing list
> > > > > Cialug at cialug.org <javascript:;>
> > > > > http://cialug.org/mailman/listinfo/cialug
> > > > >
> > > > _______________________________________________
> > > > Cialug mailing list
> > > > Cialug at cialug.org
> > > > http://cialug.org/mailman/listinfo/cialug
> > > >
> > > _______________________________________________
> > > Cialug mailing list
> > > Cialug at cialug.org
> > > http://cialug.org/mailman/listinfo/cialug
> > >
> >
> >
> >
> > --
> > Tim
> > Required reading: http://bccplease.com/
> > _______________________________________________
> > Cialug mailing list
> > Cialug at cialug.org
> > http://cialug.org/mailman/listinfo/cialug
> >
>
>
>
> --
> Jeff Chapin
> President, CedarLug, retired
> President, UNIPC, "I'll get around to it"
> President, UNI Scuba Club
> Senator, NISG, retired
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/listinfo/cialug
>



-- 
Tim
Required reading: http://bccplease.com/


More information about the Cialug mailing list