50

I have always used bash for work, and never had problems with it. Now, it does not save any command in history again. If I open a window terminal, try some commands, it acts like everything has worked fine and show me history using the arrow keys. But If I close the window (and that it is the time that it supposed to be save on the .bash_history file) and open another one, there are no signs of the last commands.

How can I find out what is going wrong? Or re-set everything from blank.

4
  • Could be your profile or rc file. What's in them?
    – user14492
    Dec 8, 2015 at 18:41
  • 2
    What are the permissions and owner on your .bash_history? Mine are 600 and owned by me. Also, what's in $HIST_FILE?
    – blm
    Dec 8, 2015 at 18:41
  • 1
    @blm Permissions are right; printing the $HIST_FILE I found out about .bash_sessions . Apparently there are problems when, at the end of a session, the OS save those file to the history file. I don't want to investigate anymore so I have simply put this in my home .bash_sessions_disable and know it return to the default behavior
    – abaini01
    Dec 9, 2015 at 8:15
  • Please consider filing a bug report with Apple: developer.apple.com/bug-reporting
    – Chris Page
    Dec 16, 2015 at 13:20

7 Answers 7

45

I did this:

Add a variable to .bash_profile file

SHELL_SESSION_HISTORY=0

restart the terminal and after that it is working as I'd like. (It saved the commands after I closed the terminal)

P.S. I also use the HISTFILESIZE and HISTSIZE variables

HISTSIZE is the number of lines or commands that are stored in memory in a history list while your bash session is ongoing.

HISTFILESIZE is the number of lines or commands that (a) are allowed in the history file at startup time of a session, and (b) are stored in the history file at the end of your bash session for use in future sessions.

4
  • 3
    Adding that one line works fine for me on El Capitan, thanks.
    – Aidan
    Oct 8, 2016 at 9:42
  • 3
    Thanks! I would love to know why this works, too, if someone happens to know. (History=0 would imply to my ignorant mind that all history will be lost, yet it seems to have the opposite effect.) Jul 19, 2017 at 23:17
  • @Sysqa Is there some "in the middle" solution? The feature that "save and restore the bash command history independently for each restored terminal session" sounds like a nice feature some program might benefit from. Is there some way to have the benefits from the previous configuration and also the new one with some kind of configuration?
    – loco.loop
    Oct 13, 2017 at 21:18
  • 2
    @MikeWilliamson, the comments in /etc/bashrc_Apple_Terminal give the answer: "The default behavior arranges to save and restore the bash command history independently for each restored terminal session. It also merges commands into the global history for new sessions. Because of this it is recommended that you set HISTSIZE and HISTFILESIZE to larger values. You may disable this behavior and share a single history by setting SHELL_SESSION_HISTORY to 0." My env currently has that var at "1", so I think they're saying "0" disables the per-session history, and "1" enables it.
    – chaserb
    Aug 24, 2022 at 21:50
10

Starting in OS X 10.11 El Capitan, the system-installed script /etc/bashrc_Apple_Terminal coordinates with Terminal in order to save/restore separate command histories for each terminal restored for Resume.

Read the comments in /etc/bashrc_Apple_Terminal for an explanation of how it manages per-terminal command histories and how to customize it.

If you customize PROMPT_COMMAND be sure to concatenate to the previous value so you don't wipe out the system-supplied command:

PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }your_code_here"

If you install an EXIT signal handler with trap be sure to do something similar (or call shell_session_update from your handler if you can't figure out how to concatenate to the previous value—it's a little involved).

When you exit the shell, this code will save new commands to the terminal's history in ~/.bash_sessions. To see if it encounters any problems, instead of closing the terminal exit the shell manually with exit (or Control-D). It logs progress messages. Note if it doesn't complete or if any sort of warning or error messages are displayed.

In general, bashrc_Apple_Terminal attempts to detect and disable per-session history if it looks like the user has performed any customizations that aren't compatible with it. It sounds like you may have found one it doesn't handle. Please consider filing a bug report with Apple: https://developer.apple.com/bug-reporting/

4
  • I haven't done any special configuration so I guess it's not the problem. Could you explain what the PROMPT_COMMAND does.
    – loco.loop
    Oct 13, 2017 at 21:21
  • @loco.loop Do you mean “What is Bash’s PROMPT_COMMAND variable for?” or do you mean “What does /etc/bashrc_Apple_Terminal use PROMPT_COMMAND for?”? If the latter, I refer you to the code, which is documented in detail. Or, you should start another Q&A about that.
    – Chris Page
    Oct 20, 2017 at 23:16
  • I mean what PROMPT_COMMAND for... @ChrisPage
    – loco.loop
    Oct 22, 2017 at 0:26
  • @loco.loop I refer you to the Bash documentation, or suggest you start a Q&A about that.
    – Chris Page
    Oct 22, 2017 at 2:10
10

On a fresh install of Mac OS X (updated to 10.13.6), bash command history was not being saved. There were also no .bashrc or .bash_profile files. In this case, just adding an empty .bashrc file fixed it for me.

touch .bashrc

That's seems to be all you need...

3
  • 1
    seems to fix the issue on Mojave / 10.14, too :-)
    – ssc
    Feb 9, 2019 at 21:15
  • 1
    Fixed for me as well, this should have more upvotes May 25, 2020 at 3:13
  • What does this do? It is surprising that this would make a difference. It may indicate a bug somewhere that should be fixed.
    – Chris Page
    Oct 3, 2022 at 19:15
9

Just in case there are others out there that have RVM (Ruby Version Manager) installed: Check to see if you have the following line in your ~/.profile, ~/.bashrc or ~/.bash_profile files.

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

It's probably RVM preventing the exit "hook" for bash_sessions to run.

That was the problem for me. Try commenting it out.

Source: Reddit

6

I had the same problem with a newly installed osx Mojave. I checked my ~/.bash_history file and saw this:

$ ls -l ~/.bash_history -rw------- 1 root staff 599 Jan 4 20:50 /Users/gilm/.bash_history

Simply chowning it back to gilm solved the problem. I used:

sudo chown gilm /Users/gilm/.bash_history

and that solved my problem.

1
  • Thanks, that worked for me. My history was lost when I switched the default shell from zsh to bash. May 11, 2022 at 22:28
3

You could also create a .bash_logout file with the following:

shell_session_update

Sourced from GitHub comment

1
  • Or, manually type trap shell_session_update EXIT once, and logout. There are clues in the /etc/bashrc_Apple_Terminal script that are too boring to include here.
    – MarkHu
    May 2, 2018 at 0:41
0

For me the solution was more hidden, yet easier to resolve.
By looking at the extended attribute of my .bash_history file and .bash_sessions content, I found out it had these attributes disallowing me to write despite the normal permissions:

$ ls -le .bash_history
-rw-r--r--@   1 XXX  staff  14484  2 Aug 17:04 .bash_history
 0: group:everyone deny write,delete,append,writeattr,writeextattr,chown

The same if running ls -lea .bash_sessions.
The solution was "simply":

chmod -R -a# 0 .bash_sessions/ .bash_history

You must log in to answer this question.

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