Bash history not initially remembered with iterm2

bashhistoryiterm

I've got an annoying problem with iterm2 not remembering my history when I close out a terminal window. I'm not sure if it might be my .bash_profile config or what.

Here is what is happening:

1) type in some commands
2) issue `history` command
3) commands are listed
4) close terminal window and open brand new terminal window
5) issue `history` command
6) commands from previous sessions are NOT listed
7) close terminal window and open brand new terminal window
6) issue `history` command
7) commands from very first session now appearing

Anyone know why the previous commands aren't showing up right away when a new window is opened and only after a second terminal window is opened?

UPDATE: Everything works as expected with the plain old terminal app.

Update 2: I have the shell integration feature installed

Update 3: It turns out the first update was incorrect. It is also flaky on Terminal. Sometimes it will work and other times it doesn't.

Best Answer

So it turns out iterm was actually working in iterm. It just waits 5 seconds before saving to .bash_history when you close out of a shell without using the exit command. This 5 second delay is to give you time to command-z the session back open, apparently.