.bash_history file - Slackware

This is a discussion on .bash_history file - Slackware ; I am finding the behavior of .bash_history irregular. According to "Learning the BASH Shell" (O'Reilly), every bash interactive session maintains it's own list of commands, and saves the list in .bash_history in your home directory when you exit from the ...

+ Reply to Thread
Results 1 to 10 of 10

Thread: .bash_history file

  1. .bash_history file

    I am finding the behavior of .bash_history irregular.

    According to "Learning the BASH Shell" (O'Reilly), every bash
    interactive session maintains it's own list of commands, and saves the
    list in .bash_history in your home directory when you exit from the
    shell. When you log in, bash is supposed to read the initial history
    list from .bash_history.

    Every command you enter is supposed to be added to this list (subject
    to any limitations that are imposed by HISTFILE, HISTSIZE and
    HISTCONTROL. However, some commands seem to get added, but not all.
    I enter a completely new command, but echo HISTCMD returns an
    unchanged command number.

    I noticed that .bash_history was listed in my home directory, for some
    reason, as belonging to root. I chown-ed it, but that didn't help.

    Any thoughts?

  2. Re: .bash_history file

    > I am finding the behavior of .bash_history irregular.
    ....
    > I noticed that .bash_history was listed in my home directory, for some
    > reason, as belonging to root. I chown-ed it, but that didn't help.
    >
    > Any thoughts?


    man bash

    HISTFILE
    The name of the file in which command history is saved (see HISTORY
    below). The default value is ~/.bash_history. If unset, the command
    history is not saved when an interactive shell exits.

    HISTIGNORE
    A colon-separated list of patterns used to decide which command lines
    should be saved on the history list.
    ...


    --
    damjan

  3. Re: .bash_history file

    On Mar 25, 7:55 pm, Damjan wrote:
    > HISTFILE
    > The name of the file in which command history is saved (see HISTORY
    > below). The default value is ~/.bash_history. If unset, the command
    > history is not saved when an interactive shell exits.


    I have HISFILE set to the default -- .bash_profile

    >
    > HISTIGNORE
    > A colon-separated list of patterns used to decide which command lines
    > should be saved on the history list.


    HISTIGNORE is not set in my environment. Therefore, I assume
    *every* command that I enter should be appended to .bash_history. But
    it isn't.



  4. Re: .bash_history file

    Vwaju wrote:
    >
    > Every command you enter is supposed to be added to this list (subject
    > to any limitations that are imposed by HISTFILE, HISTSIZE and
    > HISTCONTROL. However, some commands seem to get added, but not all.
    > I enter a completely new command, but echo HISTCMD returns an
    > unchanged command number.


    This had confused me some time back. I don't remember all the details, but
    each shell saves its own history. The last shell to be exited is the
    history that gets saved - the one you see the next time you log in.

    >
    > I noticed that .bash_history was listed in my home directory, for some
    > reason, as belonging to root. I chown-ed it, but that didn't help.


    No idea why root owned it. Maybe it had something to do with a "su" vs.
    a "su -".

    - Kurt

  5. Re: .bash_history file

    Vwaju (lou@manhattanhandyman.com) writes:
    > I am finding the behavior of .bash_history irregular.
    >
    > According to "Learning the BASH Shell" (O'Reilly), every bash
    > interactive session maintains it's own list of commands, and saves the
    > list in .bash_history in your home directory when you exit from the
    > shell. When you log in, bash is supposed to read the initial history
    > list from .bash_history.
    >
    > Every command you enter is supposed to be added to this list (subject
    > to any limitations that are imposed by HISTFILE, HISTSIZE and
    > HISTCONTROL. However, some commands seem to get added, but not all.
    > I enter a completely new command, but echo HISTCMD returns an
    > unchanged command number.
    >

    I sort of wondered about that, but I've never looked into it.

    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
    I know I hadn't run in over a month, yet something I did today wasn't
    there. So I did log out, and logged back in, and there was a more
    recent history.

    Michael

  6. Re: .bash_history file

    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.

  7. Re: .bash_history file

    On 2008-03-26, slakmagik 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

  8. Re: .bash_history file

    On Wed, 26 Mar 2008 06:36:11 -0500, No_One wrote:
    > On 2008-03-26, slakmagik 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.


  9. Re: .bash_history file

    On 2008-03-26 Wed 07:36:11, No_One wrote:
    > On 2008-03-26, slakmagik 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.

  10. Re: .bash_history file

    On 2008-03-26 Wed 08:52:27, ddd wrote:
    > On Wed, 26 Mar 2008 06:36:11 -0500, No_One wrote:
    >> On 2008-03-26, slakmagik 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.

+ Reply to Thread