4

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.

6
  • Without further details, this could be a duplicate of apple.stackexchange.com/questions/50649/… (linked on top as well), apple.stackexchange.com/questions/139807/… or one of the other similar questions here. Why don't you either try the suggestions in the answers to the linked questions or edit more details about your detailed setup and what you've already tried into the question?
    – nohillside
    Commented Dec 25, 2015 at 13:42
  • @patrix Sorry about that. I already tried the suggestions in the question now linked above before posting this question. I added more information to my question.
    – user162962
    Commented Dec 25, 2015 at 15:45
  • 1
    Found a solution. I would have added it as an answer, if this question hadn't been locked. I had to create the following file ~/.bash_sessions_disable in order to disable behaviour introduced in El Capitan. superuser.com/questions/950403/…
    – user162962
    Commented Dec 25, 2015 at 15:56
  • The question’s not actually locked as far as I can tell. So I think you should be able to post your answer here (and if you do, I’ll upvote it, because while there’s already an answer for it a SuperUser, there isn’t one here at Ask Different yet). Commented Dec 25, 2015 at 17:18
  • Nothing suggested here seems to work on 10.15.5 with iTerm2 3.3.9. History is lost on close. Commented Jun 2, 2020 at 10:16

3 Answers 3

1

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.

0

See this answer from a similar question. El Capitan made some changes whereby it tries to do session-dependent history. You can likely fix your problem just by running touch ~/.bash_sessions_disable to create a flag file to disable this behaviour.

0

I tried using all solutions/tricks together but it didn't work. I removed a few (may be they were creating conflict). This is my setup that worked. Sonoma 14.6

  1. Remove HISTFILE env variable if you have one, even if it points to correct file as in ~/.bash_history
  2. I tried and removed SHELL_SESSION_HISTORY=0. Remove it if you have this env variable.
  3. chown ~/.bash_history to yourself (from root)
  4. touch ~/.bash_sessions_disable

You must log in to answer this question.