Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: .zsh_history



On Sat, Apr 8, 2023 at 11:17 AM Perry Smith <pedz@xxxxxxxxxxxxxxxx> wrote:
>
> I don’t have anything special set up other than setting HISTFILE, HISTSIZE=2097152, and SAVEHIST=1048576.  I also have histignoredups and histignorealldups on.
>
> The reason for the huge numbers is partially due to some confusion I had (see below) but basically I want to save unique commands that I did for very long periods of time so I don’t need to figure them out again.

I do the same, but hopefully you are not relying on zsh to do that,
because you have to grep the history file yourself.

> I’m on macOS and they have this thing for “sessions” which I’ve turned off now (a few weeks ago).  The reason is that the session history files are limited to two weeks and so commands I did long ago are getting deleted and my .zsh_history file was only growing to be about 1000 lines.
>
> My question is, if I start multiple terminals and run various commands and eventually they each log out or exit, I’m wondering what happens to the .zsh_history file.  Does the last shell to exit rewrite the file using its own history which (I’m guessing) is the contents of .zsh_history when the shell started up plus the commands that were executed in that shell?  This would overwrite all the commands done in the other shells that have just exited.

It writes all the current history of the shell (`fc -l 0`), but by
default APPEND_HISTORY is on, so it would append the current history
to the file. So in practice the history is not overridden.

But is this realistic? Do you actually have more than 1048576 lines in
your history? What is $HISTCMD?

> Alternatively, the shell could re-read .zsh_history at the time it is exiting and append the commands that were executed within that shell and write the result out — thus preserving the commands done by other shells.

That's exactly what it does (unless you set NO_APPEND_HISTORY).

Cheers.

-- 
Felipe Contreras




Messages sorted by: Reverse Date, Date, Thread, Author