I like to save every history in bash to a kind of logfiles, and made an extra directory ~/lib/hists/ for that purpose.
Writing such a logfile in the shell is easy:
history -w /home/stefan/lib/hists/bash-hist-$(date +%F_%T_%N)
Now, if I shutdown, all closing xterms shall write their logfile, which is, why I include the nanotime in the filename, to avoid collision.
So I put that command into ~/.bash_logout
Unfortunately, closing an xterm didn't generate such a logfile. I thought, maybe because it's because I closed the window. So startet bash in bash:
bash ls exit
but again, no history written.
Most of the time I have 4 to 6 open terminals but only one history file for which I don't know, which commands are ending there - from the first opened shell, from the last closed shell? While working, the histories don't interfere, so I guess it is only bound to one shell instance.
I'm interested in explanation, why my approach fails, how I can fix it, including very different approaches.
shopt login_shell. Edit: See Andy's link above.
/etc/bash.bash_logout. Note the caveats - in particular, it is not a secure audit log, and is not intended to be. Also, it has been some time since i wrote/used this and it may have suffered from bit-rot.