• Re: bash history

    From Greg Wooledge@21:1/5 to mick.crane on Sun Jul 28 00:10:01 2024
    On Sat, Jul 27, 2024 at 22:50:17 +0100, mick.crane wrote:
    In debian bookworm, xfce desktop, different virtual terminals have a different history if same user presses "up key" in different virtual terminals ?

    As your subject says, this is "bash history". And yes, each instance of
    bash has its own separate history. It has nothing to do with your
    terminal emulator or your desktop environment.

    Bash reads its history from a file (~/.bash_history by default) at
    startup time, manages it in memory while running, and writes it back
    out to the history file upon exiting.

    Is this something that can be changed so history is shared between virtual terminals?

    No.

    I consider this a *good* thing, personally. I used commerical Unix with
    ksh for a very long time, and ksh shares history between instances in
    real time. This is a *nightmare*. I really, really disliked it.

    In the worst case scenario, you and a coworker might both be logged in
    as root (where root's shell is ksh) simultaneously. Your histories
    would then intermix in real time. You might press ESC k ENTER in your terminal, expecting to re-run the command you had just executed a
    moment ago, and unexpectedly run your coworker's command.

    Even in a "normal" scenario where I'm logged in as myself, and nobody
    else has my password, I would not want all of my terminal windows to have
    a shared history. Each of them is doing something different. I want
    each terminal's workflow to be isolated to that terminal and what I'm
    doing in it. I don't want them contaminating each other.

    Of course, that's my opinion, and if you want a shared history across concurrent shell instances, there are probably shells that can do that.
    I've never looked for it, because having lived with it, I already know
    I don't want it again.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andy Smith@21:1/5 to mick.crane on Sun Jul 28 00:40:01 2024
    Hi,

    On Sat, Jul 27, 2024 at 10:50:17PM +0100, mick.crane wrote:
    Is this something that can be changed so history is shared between virtual terminals?

    You may be interested in "atuin" to aggregate shell history from
    multiple logins and machines in a searchable interface. It can be
    self-hosted.

    https://www.youtube.com/watch?v=89zPvQF3tas

    Thanks,
    Andy

    --
    https://bitfolk.com/ -- No-nonsense VPS hosting

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Wooledge@21:1/5 to mick.crane on Sun Jul 28 01:00:02 2024
    On Sat, Jul 27, 2024 at 23:44:08 +0100, mick.crane wrote:
    On 2024-07-27 23:08, Greg Wooledge wrote:
    On Sat, Jul 27, 2024 at 22:50:17 +0100, mick.crane wrote:
    In debian bookworm, xfce desktop, different virtual terminals have a different history if same user presses "up key" in different virtual terminals ?

    As your subject says, this is "bash history". And yes, each instance of bash has its own separate history. It has nothing to do with your
    terminal emulator or your desktop environment.
    Where is it if not in ~/.bash_history?

    Then it's wherever you changed it to.

    Bash reads its history from a file (~/.bash_history by default) at
    startup time, manages it in memory while running, and writes it back
    out to the history file upon exiting.

    Does this separate history get written to ~/.bash_history when terminal emulation thing is closed?

    You need to specify *exactly* what you're doing.

    If you close a terminal emulator in such a way that it kills the shell
    without letting the shell write history to disk, then the history will
    not be saved.

    If you want history to be saved, you should exit from the *shell*
    (e.g. by pressing Ctrl-D), rather than closing the terminal and counting
    on that to terminate the shell in a way that will preserve history.

    If you *don't* want history to be saved, there are many ways to make
    that (not) happen, depending on your terminal emulator and so on. You'll
    need to experiment, though, to see exactly what happens when you do
    whatever it is you do.

    Whenever I need to reboot my computer (kernel update or the like), I
    decide which shells I want to retain history from, and I exit from those cleanly (^D). I leave the others running, and exit from FVWM, which
    kills them in a way that causes their history not to be written.

    If you want a particular shell window's history to be AGGRESSIVELY
    written to disk every time you run a command, you can arrange for that
    as well. See <https://mywiki.wooledge.org/BashFAQ/088> for example.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From The Wanderer@21:1/5 to mick.crane on Sun Jul 28 01:10:01 2024
    This is an OpenPGP/MIME signed message (RFC 4880 and 3156)
    On 2024-07-27 at 18:44, mick.crane wrote:

    On 2024-07-27 23:08, Greg Wooledge wrote:

    On Sat, Jul 27, 2024 at 22:50:17 +0100, mick.crane wrote:

    In debian bookworm, xfce desktop, different virtual terminals
    have a different history if same user presses "up key" in
    different virtual terminals ?

    As your subject says, this is "bash history". And yes, each
    instance of bash has its own separate history. It has nothing to
    do with your terminal emulator or your desktop environment.

    Where is it if not in ~/.bash_history?

    In memory.

    Bash reads its history from a file (~/.bash_history by default) at
    startup time, manages it in memory while running, and writes it
    back out to the history file upon exiting.

    As Greg wrote: bash reads it in at startup time, then manages it in
    memory, then writes it back out when exiting.

    Unless you've configured it otherwise, the place it reads the history in
    from and writes it out to is ~/.bash_history.

    In between those, each and every separate instance of the shell keeps
    the history in memory. Yes, if I'm not mistaken that *does* mean that if
    you have 20 terminals open you have 20 copies of (some version of) the
    history in memory.

    Does this separate history get written to ~/.bash_history when
    terminal emulation thing is closed?

    That depends on how you have things configured. It's not really
    "separate", however; it's just that history is only written out when
    bash exits (sufficiently cleanly), and that's just as true when you have multiple instances of bash as when you have only one.

    --
    The Wanderer

    The reasonable man adapts himself to the world; the unreasonable one
    persists in trying to adapt the world to himself. Therefore all
    progress depends on the unreasonable man. -- George Bernard Shaw


    -----BEGIN PGP SIGNATURE-----

    iQIzBAEBCgAdFiEEJCOqsZEc2qVC44pUBKk1jTQoMmsFAmalfYkACgkQBKk1jTQo MmuD4w//Xrla9UhNVhUGpJvl1jvqBUVQpTdkkZP4rYF1pppqcrNqfE2kZI5EKsLc hzz0Q1L6qmgwXma+HoGfZVAY8az5ja49lwoZRTfevfb9nm9SsGPG8ADeB06sCxkH IM0V9/9KjkU1FCzfRZ8rraBUsVMPt6kuc3CawreQPGOOiRSbQfr+tX2DonpgVFS+ EifCoKf73rY82yx9sjPlFwAOdiMoCQdd3e5GsmrNacxl3JZ2P6D+fWgNYrpMHPyA B3DhLfH1WkKmZbKhHJqn2RHQGbNHaAFdM2PHE4r8atlWDt7II4rRnBX4xw2zxk6z EKMA2F5XWpQqYWTRvXCqJR37KH8DgVgKVa1SBY3drkeW2rzNELKyaGGCKybP5rh2 ChSsxlOlCVSoS156ekNCrJXu83l+6FXA1TSeQV3NyTtYP0kTzRpYITB9/5B2cnPD ZY2xgz70DcxBM34RkHLN11TojBIWEMZCivMvxYqt/hQhDoS+6aXg7SpWLEyLL+j2 COrr35VQ1OpMLL4iiSRs6ni5DYrGk/AZVh2H61yAcixMh9vklzHYb0wAZhhLseQh uhckYK2Lvn8Mw66iPvvM+u6PBH2fZInkIxq9/GvYB/NAMrZPWfK1CHQn6jGFzPzh BwWJN2Nu6gCvb2pBj7aVVQPwrZP5
  • From Vincent Lefevre@21:1/5 to mick.crane on Sun Jul 28 02:30:01 2024
    On 2024-07-27 22:50:17 +0100, mick.crane wrote:
    In debian bookworm, xfce desktop, different virtual terminals have a different history if same user presses "up key" in different virtual terminals ?
    Is this something that can be changed so history is shared between virtual terminals?

    This is possible with zsh (option SHARE_HISTORY). But I do not use
    this option: if I want to retrieve a command from another terminal,
    I simply re-execute the shell to get the whole history of commands
    typed so far (but there are other ways to manipulate the history).

    --
    Vincent LefΦvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
    100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
    Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Wooledge@21:1/5 to mick.crane on Sun Jul 28 03:20:01 2024
    On Sun, Jul 28, 2024 at 02:01:04 +0100, mick.crane wrote:
    On 2024-07-27 23:58, Greg Wooledge wrote:
    You need to specify *exactly* what you're doing.

    Sometimes I forget where I was after closing a virtual terminal and it
    would be handy to see the history
    in a new terminal, where I "cd'd" to for example.
    stuff like that.

    No, I mean *how did you close the terminal*? And what terminal is it?

    In the ideal scenario, if you closed the terminal by pressing Ctrl-D
    in the shell, the history should be written to disk. Then, opening a
    new terminal+shell should read that history into memory. You should be
    able to search the previous shell's history in that new shell.

    There are lots of ways this could fail. For example, you might be
    closing the terminal window by using a window manager control button,
    or by running the xkill(1) program and then clicking the window.

    Or, you might have too many history lines in memory at the time you
    exit the shell, and the cd command that you wanted to find might be
    dropped because it was too far in the past. (Bash only stores 500
    lines of history by default.)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Yassine Chaouche@21:1/5 to All on Sun Jul 28 11:30:01 2024
    Le 7/28/24 à 05:24, Mike Castle a écrit :
    On Sat, Jul 27, 2024 at 2:50 PM mick.crane <mick.crane@gmail.com> wrote:
    Is this something that can be changed so history is shared between
    virtual terminals?

    Yes.

    [...]

    From my .bashrc file, I have the following history related settings:
    # No limit on running shell history size
    HISTSIZE=-1
    # No limit on lines saved in history file
    HISTFILESIZE=-1
    # Enable timestamps in bash_history
    HISTTIMEFORMAT="[%F %T %z] "
    # Stop history being clobbered if there are multiple shells open
    shopt -s histappend
    # Immediately append history
    PROMPT_COMMAND='history -a'

    If you really want to try sharing history immediately between shells, use this:
    PROMPT_COMMAND='history -a; history -n'

    Note that PROMPT_COMMAND executes before printing a prompt, which
    means after executing a command. So, if using "history -n", then
    other shells will not load the shared history until a new prompt is
    printed (e.g., hitting the ENTER key to display a new prompt).

    For the record, I deal with the expected conflicts when merging
    history files across machines by using a simple python program that
    parses the history file (that includes the timestamps), discards the
    conflict markers, orders by timestamps, and writes it back out. It is
    by no means perfect, but "good enough for me".

    For those worried about the unbounded history, I started doing that
    about ten years ago and my work history is currently just shy of
    180,000 commands. It would likely be less if I turned on the
    "erasedups" feature, but I like to keep the context. And I've seen
    comments about folks who have multiple decades of shell history. On
    modern machines, it simply isn't an issue.

    mrc

    I second Mike Castle on this.

    I have more or less the same config for my bash:

    export HISTSIZE=-1 # write all commands to history file
    export HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
    export HISTFILESIZE=-1 #don't truncate
    export PROMPT_COMMAND="history -a; history -n" # [a]ppend commands to histfile immedatly,
    # and read [n]ew entries (could be written from other terminals)

    The PROMPT_COMMAND is where the magic happens:
    Every time you press Enter,
    your command is instantly recorded to the history file with history -a,
    instead of waiting until bash exits properly.
    This ensures you never lose track of any command,
    even if your bash session ends unexpectedly.
    Additionally, history -n allows your shell to read the bash history again, updating it with any new entries.

    Imagine executing foo in terminal 1,
    pressing Enter,
    then switching to terminal 2.
    By simply pressing Enter again and hitting the Up arrow,
    you'll get the command foo.

    This seamless synchronization across terminals enhances my productivity by ensuring all terminals are always in sync.

    Best,


    --
    yassine -- sysadm
    +213-779 06 06 23
    http://about.me/ychaouche
    Looking for side gigs.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From songbird@21:1/5 to mick.crane on Sun Jul 28 14:20:01 2024
    mick.crane wrote:

    In debian bookworm, xfce desktop, different virtual terminals have a different history if same user presses "up key" in different virtual terminals ?
    Is this something that can be changed so history is shared between
    virtual terminals?
    mick

    not that i would want that,

    but it would be possible for various terminals to save to
    their own unique history files based upon terminal pty or
    tty or anything else you'd like and to reload those upon
    starting up again.

    to keep my own setup consistent and to not keep certain
    things in history i actually do the opposite of what you
    want because i want certain commands already preloaded in
    my history for all windows when i start up and then i adjust
    my environment based upon which pty or directory i'm in.

    i also consider it a security or privacy leak to keep
    some information in history files of any kind so those get
    cleaned up upon normal shutdowns or start ups.


    songbird

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Yassine Chaouche@21:1/5 to All on Sun Jul 28 15:30:01 2024
    Le 7/28/24 à 12:19, songbird a écrit :
    [...]
    to keep my own setup consistent and to not keep certain
    things in history i actually do the opposite of what you
    want because i want certain commands already preloaded in
    my history for all windows when i start up and then i adjust
    my environment based upon which pty or directory i'm in.

    I'm scratching my head here.
    Can anyone provide a solid example or reason why preloading commands in history is necessary?

    Best,

    --
    yassine -- sysadm
    +213-779 06 06 23
    http://about.me/ychaouche
    Looking for side gigs.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From The Wanderer@21:1/5 to Yassine Chaouche on Sun Jul 28 15:50:02 2024
    This is an OpenPGP/MIME signed message (RFC 4880 and 3156)
    On 2024-07-28 at 09:29, Yassine Chaouche wrote:

    Le 7/28/24 à 12:19, songbird a écrit :

    to keep my own setup consistent and to not keep certain things in
    history i actually do the opposite of what you want because i want
    certain commands already preloaded in my history for all windows
    when i start up and then i adjust my environment based upon which
    pty or directory i'm in.

    I'm scratching my head here.

    Can anyone provide a solid example or reason why preloading commands
    in history is necessary?

    It's never *necessary*, AFAIK, but it *is* often convenient. (And in
    many things, convenience is *the* deciding factor.)

    It's often *far* easier to repeat a lengthy command from history (e.g.
    via Ctrl+R and typing part of the command), either verbatim or with modifications, than by retyping it from scratch. I rely on this
    extensively - more in my terminal-based SQL-database interaction
    sessions than in the shell itself, because the commands there are far
    more complex than those in the shell, but I certainly do also rely on it
    in the shell.

    If you expect to want to use similar commands repeatedly over multiple
    shell sessions, why wouldn't you want them there in the history for
    ready access?

    --
    The Wanderer

    The reasonable man adapts himself to the world; the unreasonable one
    persists in trying to adapt the world to himself. Therefore all
    progress depends on the unreasonable man. -- George Bernard Shaw


    -----BEGIN PGP SIGNATURE-----

    iQIzBAEBCgAdFiEEJCOqsZEc2qVC44pUBKk1jTQoMmsFAmamS9MACgkQBKk1jTQo MmuPOA/+KJlfL46sulf36bIsWSZZWtOJkiahM1QTgWrIemrIdfa23GascxbBPK67 PLufLMTfezHuAfoltuup59Jutjg3KNNd9YYXz5TMUmDFuiKKZdf01mPZPg61Hc5s 6z02jRxjxKVRqi9Q9sRw9xMrIWUPXjg/ZVUtjMTHl2z+7jAd66BDxElS3j6Z/55c eNLHBTP1zaN15Ui2zhpYDVxBv7KXkRz3zO5aYaLNN6WEqz588YP7esWtxTWliixj m/fZ6zxBT5xxr4Yh+rQrPT3roKbTXgOWwSPLiroGBYFB63EWjvifi+C5wow+fZUw JCn//tI5E6mYP2cLVbZzch+GOZ8yhowdjz02n217U4xDAIXWo9XLsdC7YyFBiD4a xlKDHBVNub2yfoZYsefNvM/oErLIYGYVX1bBKdTsdXauh3WASeA0/vQNMJEJ61tg Y2K9CLUV4baMvI70xb7DMXyUu5xRrUnAwW+sd+SGpeFqtyqH6lab5IyFyHXhkI9B VOTLVVen43GagpzYooQxZuzASePp5VkSahED5OmymylKU5BlPuUpgww5Wc05AlTT 9g/rIF45xBtSPeTPFv+deuPD/jJ3wWR3kZ2D5BUKGVJEWz3ghWW9CLbPQiusrgfc 5WeGjLYEYdgvdWkUDqn+aeeyAz5R
  • From Karl Vogel@21:1/5 to All on Thu Aug 1 11:50:01 2024
    This is how I keep a long-term record of bash commands from different
    sessions:
    https://www.reddit.com/r/bash/comments/ak9c3r/

    HTH

    --
    Karl Vogel I don't speak for anyone but myself

    Comment: I use a screwdriver a lot
    Reply: I'm all out of orange juice. Will straight vodka work?
    --Reddit "What tool helped you as an early sysadmin?"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)