Bash history is not remembered in Terminal

bashsshterminal

My Bash history is remembered when I connect to my Mac via SSH, use iTerm2 or launch bash in the built-in Terminal app (every command I typed in bash is saved) but is not when I just use the built-in Terminal app. I already tried resetting the profiles in Terminal. I also checked ownership and permissions of .bash_history. Later I also tried to delete this file. This answer didn't help either. My default shell definitely is bash.

Best Answer

Begin isolating the source of the problem by removing any customizations of Bash’s history. For example, any code that sets shell variables that begin with HIST, or uses set -o to set either the history or histexpand shell options.

The per-session command history code in /etc/bashrc_Apple_Terminal attempts to maintain compatibility with Bash’s history variables, and tries to detect when the user is probably doing something it can’t be compatible with and disables the mechanism in that case (because the user is doing something arbitrary, like loading, saving or modifying the history). However, if you have scripts that run the history command, that code has no way of knowing in advance; you’ll need to either update your code to maintain compatibility with the new per-session command history, or disable the per-session history by setting SHELL_SESSION_HISTORY=0 as described in the documentation in /etc/bashrc_Apple_Terminal.

If you’re not customizing HISTFILE, HISTTIMEFORMAT or the histappend shell option, another thing to check is if your shell startup script(s) modify either PROMPT_COMMAND variable or the EXIT trap without concatenating the previous value to avoid wiping it out.

See the comments in /etc/bashrc_Apple_Terminal for a description of how the mechanism works and how to customize or disable it.