From: slakmagik on
On 2008-03-26 Wed 07:36:11, No_One wrote:
> On 2008-03-26, slakmagik <j(a)hostname.invalid> wrote:
>> On 2008-03-25 Tue 22:15:59, Michael Black wrote:
>>> One key factor is that .bash_history only gets updated when you log
>>> out. I just looked, and there were some entries for commands that
>>
>> Veering slightly off-topic but you can set
>>
>> export PROMPT_COMMAND="history -a"
>>
>> to have bash append to the history file each time it prints the prompt.
>> The only problem with that is that shell commands from multiple
>> instances will be interwoven, if that bothers you.
>
> Slightly more off topic, I guess...I use multiple VTs and need to save the
> commands from each VT....bash doesn't let you do this so I added to .bashrc:
>
> TTYSCREEN=$(tty|sed s-/dev/tty--)
> HISTFILE=/root/$TTYSCREEN.history
>
> then export HISTFILE
>
> That way when I start a new vt, the history is saved to 1.history or
> 2.history, depending on the number of the VT.
>
> ken

I meant to reply to this a long time ago but got busy.

Bash does let you save history from all VTs - but, true, not into
separate files.

In a consistent CLI environment it doesn't matter but it doesn't work
from a pseudo-terminal as unprivileged user - it produces
'/root//dev/pts/13.history'

Perhaps

HISTFILE=$HOME/.bash_history$(tty|sed -n s-/dev/tty-.-p)

which produces a default '$HOME/.bash_history' when tty doesn't return
'/dev/tty' and produces $HOME/.bash_history.N when it does. Only problem
with *that* is that it doesn't even work from a VT because my VTs have
the device name of vc/N instead of ttyN. To get around that, maybe

HISTFILE=$HOME/.bash_history$(tty|sed -n /pts/!s-.*/-.-p)

though, of course, that'll match anything *but* pts, well beyond just
vc/tty, which may not be desired either.

Probably the most general is just to have separate files for everything
with something like:

HISTFILE=$HOME/.bash_history$(tty|sed s,/,.,g)

and, since it's bash, we could ditch sed altogether:

TTYFILE=$(tty)
HISTFILE=$HOME/.bash_history${TTYFILE//#*\//.}

Anyway - just some thoughts. I like having one history file, personally
(well, one active and one archive), but it's a really neat idea.
From: slakmagik on
On 2008-03-26 Wed 08:52:27, ddd wrote:
> On Wed, 26 Mar 2008 06:36:11 -0500, No_One <no_one(a)no_where.com> wrote:
>> On 2008-03-26, slakmagik <j(a)hostname.invalid> wrote:
>>> On 2008-03-25 Tue 22:15:59, Michael Black wrote:
>>>> One key factor is that .bash_history only gets updated when you log
>>>> out. I just looked, and there were some entries for commands that
>>>
>>> Veering slightly off-topic but you can set
>>>
>>> export PROMPT_COMMAND="history -a"
>>>
>>> to have bash append to the history file each time it prints the prompt.
>>> The only problem with that is that shell commands from multiple
>>> instances will be interwoven, if that bothers you.
>>
> Also you can try:
>
> shopt -s histappend
>
> this will let each bash instance append to the history file instead of just
> overwriting it.
>

I meant to reply to this a long time ago but got busy.

Good catch - I forgot to mention that and it's pretty important. Also
unsetting HISTFILESIZE and so on.