Join the discussion

Write your take first — we'll ask for email only when you're ready to publish.

  • Hacker News
  • Now run AI on zsh, ask it why history would be lost and see if it can find it with no hints.
  • There is a whole section in the article about that
  • A very interesting investigation, thanks for sharing
  • The eval bit in the end is interesting. Opus 5 and Sol 5.6 solved it reliably but Opus used 10x more tokens and room 10x as long!
  • Nice work! I have felt like I lost zsh history before but never looked into it.
  • I have lost commands in the Bash history as well and I don't know why. Fortunately, I store all commands executed (+ time, return value etc.) in an sqlite database so I run a program from time to time to import that into the Bash history. The Bash history is useful for Ctrl+R
  • That's a bug? Happens to me with bash all the time, all I have to do is open 2 terminals at the same time... whichever one I close last is the one that writes its history. /shrug
  • That's what zsh's INC_APPEND_HISTORY (mentioned in TFA) avoids. Bash may have an equivalent.
  • This is exactly why I stopped using Bash around 2012. I was soooo tired of losing random shell history.

    If you haven't looked into ZSH, it has some really nice customizations. Notably, from my config[1]:

      # Remove dups
      setopt HIST_FIND_NO_DUPS
      setopt HIST_IGNORE_DUPS
      # Don't  log commands starting with a space
      setopt HIST_IGNORE_SPACE
      # Replcae bang-command !34 with the actual command in the history
      setopt BANG_HIST
      # Remove extraneous spaces
      setopt HIST_REDUCE_BLANKS
      # Write the history file in the ":start:elapsed;command" format
      setopt EXTENDED_HISTORY
      # Record run-time of the command
      setopt INC_APPEND_HISTORY_TIME
    
    [1] https://doc.xn0.org/.zshrc
  • Running bash inside zsh continuously destroyed my history for the last 3 years until I figured that HISTFILE was exported and inherited by bash.
  • Does zsh have that 2000 command limit by default?

    You can't expect much from a system that has those defaults, I'm all for traditions, but if I were to depend on a system for command history, it wouldn't be anything that reads a bash_history, a revolution is needed, not incremental optimizations

    P.s: what I do is set it to unlimited on both size and line count, but it's not a forensic grade audit trace, we'd need an OS, or ssh command logger, preferably one that writes to an external disk, but it can be to a root controlled file, definitely not to a user owned file.

    Security protects not only against attackers, but against bugs, a user's command history should not be deletable by user. Anything else is a wrong architecture that we are just carrying by inertia and laziness.

  • I ran into this a few years ago (or I ran into a very similar bug). I started backing up my history file regularly because it was so annoying to lose all that history. I'll keep backing it up, but this fix is appreciated!
  • this is an example of overcomplicated solutions that could be avoided with the tool we already have for years - file system - why do this "heroic" effort of "cleverly" putting everything in one file from many sessions if we could just write command history of every session into a new separate file in a directory and read all history files from the dir instead of reading one file.
  • Or replace fopen with a SQLite database :-) like IPython does [1], but now it becomes more complicated to read and edit it including from other programs, say a text editor [2].

    [1]: https://ipython.readthedocs.io/en/stable/interactive/referen...

    [2]: https://stackoverflow.com/a/39415188

  • zshrc for saving history:

      mkdir -p $HOME/zsh
      HISTFILE="$HOME/zsh/$(date '+%y:%m%d:%H%M')-$$"
      echo "HISTFILE $HISTFILE"
    
    for easily accessing the history without running grep... will figure it out later :)))
  • >why do this "heroic" effort of "cleverly" putting everything in one file from many sessions if we could just write command history of every session into a new separate file in a directory

    For "normal" use in the terminal, a unified single history file is easier to deal with. For example, yesterday I was doing some forensics on the existence of mystery files in my backups from years ago and it was nice just loading up a single history file in the editor ... page up and page down repeatedly to eventually reconstruct the sequence of commands that explained the mystery files. The alternate idea of separate history files is less ergonomic for that. I'd have to open each history file (potentially hundreds) one by one in the editor. Trying to treat separate history files as "virtual single file" by using grep on the whole batch wouldn't work because I didn't know ahead of time what string to grep for. It required manually eyeballing the commands to consider which ones were the culprit. (Yes, if history files were separate, I could also concatenate "cat" them all to a temp file and load that into an editor ... but a history file that's already unified means I don't have to bother with that step and delete that temp file after I'm done.)

    >HISTFILE="$HOME/zsh/$(date '+%y:%m%d:%H%M')-$$"

    However, for "not normal" usage of a shell in a coding editor, I do create separate history files with a timestamp like your suggestion. In dev environments, I tend to create lots of dirty throwaway shell commands and I don't want them polluting the "normal" shell history.

  • I have nearly a decade of zsh history. Reading that article I came to the conclusion that I may have been hit by that bug in the past but I haven’t noticed.

    Then I kept reading and the author mentions accidentally exporting HISTFILE[1] and I screamed in terror and ran to my computer as I realized a mistake I’ve been making for…ever.

    I am now both happy and sad I read this article.

    [1] https://github.com/stapelberg/configfiles/commit/32dcda0f49a...

  • Not sure i understood this. What does it mean? :D

    Does it put `export` statements from the histfile into the current env?

  • I'm always curious what people get out of their terminal history. About 98% of mine is embarrassing and made for the ether.

    Have you ever looked back on your history?