Save Bash shell history also when closing Terminal tab

bashcommand lineterminal

When a macOS High Sierra Terminal shell is ended using the exit command the Bash shell history is saved and can be recalled in subsequent shell sessions. However, if the shell is ended by closing the Terminal tab or window, the shell history will not be saved. How can the shell history also be saved when closing a Terminal tab?

Presumably the problem relates to the fact that the trap set in /etc/bashrc_Apple_Terminal (trap shell_session_update EXIT) is not triggered when closing a tab. Is there a signal that could be trapped when closing a Terminal tab?

Best Answer

This drove me crazy for months until I read through /etc/bashrc_Apple_Terminal. trap shell_session_update HUP fixed it for me. Terminal appears to send SIGHUP to the shell whenever a tab is closed or the entire Terminal app is closed via Cmd-Q.