1

I recently switched from bash to zsh and want to locate the file where zsh stores the history

In the main folder howevere, although there is a .bash_history There is no zsh_history or .zhistory. I also try to locate them without success. And if I hit the uparrow key there it appears that there is a saved history somewhere

Does somebody have an Idea

1 Answer 1

7

The history file for the zsh shell is $HISTFILE:

% ls -l $HISTFILE
-rw-------  1 kk  kk  352704 May  8 14:16 /home/kk/.zsh_history

If this variable is unset, no history will be saved. There is no default value for this variable.

The shell has an in-memory history. This is what you scroll through with your arrow keys. The in-memory history will obviously be lost once the shell session terminates.

A possible value to set this variable to (in your .zshrc file) is

HISTFILE=${ZDOTDIR:-$HOME}/.zsh_history

i.e, to $ZDOTDIR/.zsh_history if the variable ZDOTDIR is defined and not empty, or to $HOME/.zsh_history otherwise. This variable does not need to be exported as it is only used by the current shell session.

The ZDOTDIR variable determines where the .zshrc and other files are picked up from ($HOME is used if ZDOTDIR is not set).

You may also want to look up SAVEHIST and other history-related shell variables in the zsh manuals.

3
  • Many thanks @Kusalananda
    – ecjb
    Commented May 8, 2020 at 12:36
  • You also need to set the SAVEHIST variable to the maximum number of entries you want to save in that file ($HISTSIZE for the size of the in-memory history defaults to 30) Commented May 9, 2020 at 13:39
  • To clarify: zsh won't save the history if you don't set the SAVEHIST variable. Contrary to HISTSIZE, it doesn't get a default value if you don't set it. Commented May 10, 2020 at 8:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .