JP Vossen on 13 Sep 2007 16:58:45 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Bash History question


Date: Thu, 13 Sep 2007 09:22:28 -0400
From: Dan Widyono <dan@widyono.net>
Subject: Re: [PLUG] Bash History question

On Thu, Sep 13, 2007 at 02:00:14PM +0100, Stephen Gran wrote:
On Thu, Sep 13, 2007 at 08:00:07AM -0400, Mag Gam said:

Just started to use Linux exclusively, and I have a question pertaining Bash History. I know the history is logged in .bash_history or ($HISTFILE var). My question is, when I fork a new shell ie. $ echo $$ 2232 $ bash $ echo $$ 4434

Where is the history kept for bash.4434? and what happens to the history of
bash.2232? How does this work, very confusing to me....

In addition to Stephen's answer regarding the writing of the history, each shell process holds its own idea of its command history in memory. As Stephen mentioned, it isn't written to disk until logout.

Since each process has its own idea of memory (controlled by the virtual
memory management of the kernel), PID2232 will have one idea of the history
"array" while PID4434 will have a separately maintained idea.

I'll add a bit to the two great answers already given.

If you are interested in synchronizing the history across different sessions/shells/pids or however you want to think about it, you can. At a high level, there are two ways to do it: manually or "automatically." <Shameless plug: this is basically recipe 16.11 of _bash Cookbook_>

For the manual method, you simply use the built-in bash tools to sync into the current shell:
history -a
history -n
--or-- "history sync"
alias hs='history -a ; history -n'

'-a' means to append history lines from this session to the history file.
'-n' means to read all history lines not already read from the history file and append them to the history list.


For the automatic method, you can use:
	PROMPT_COMMAND='history -a ; history -n'

The problem here is that $PROMPT_COMMAND contains a command to run just before the prompt is displayed, *every time*. So anything you put there gets run a *lot* and may slow down an older or heavily used system. I tend to use a history with 5000 lines, so I personally would not use $PROMPT_COMMAND.

Also, the manual method has a bit of a chicken and egg problem in that if you sync shell_1, then shell_2, then shell_3, 1&2 won't have 3's history. You basically have to loop through them all twice at one time to be sure.


See also 'help history' from the bash command line.

Hope this is useful,
JP
----------------------------|:::======|-------------------------------
JP Vossen, CISSP            |:::======|        jp{at}jpsdomain{dot}org
My Account, My Opinions     |=========|      http://www.jpsdomain.org/
----------------------------|=========|-------------------------------
Microsoft has single-handedly nullified Moore's Law.
Innate design flaws of Windows make a personal firewall, anti-virus
and anti-malware software mandatory. The resulting software arms race
has effectively flattened Moore's Law on hardware running Windows.
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug