24

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.

  • Could be your profile or rc file. What's in them? – user14492 Dec 8 '15 at 18:41
  • 1
    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 '15 at 18:41
  • @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 '15 at 8:15
  • Please consider filing a bug report with Apple: developer.apple.com/bug-reporting – Chris Page Dec 16 '15 at 13:20
25

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.

  • 2
    Adding that one line works fine for me on El Capitan, thanks. – Aidan Oct 8 '16 at 9:42
  • 2
    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.) – Mike Williamson Jul 19 '17 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 '17 at 21:18
7

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/

  • 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 '17 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 '17 at 23:16
  • I mean what PROMPT_COMMAND for... @ChrisPage – loco.loop Oct 22 '17 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 '17 at 2:10
6

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

2

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

shell_session_update

Sourced from GitHub comment

  • 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 '18 at 0:41
2

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...

  • seems to fix the issue on Mojave / 10.14, too :-) – ssc Feb 9 at 21:15
2

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.

You must log in to answer this question.

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