51

I recently replaced my Ubuntu 14.04 installation with Ubuntu Gnome (which comes with Gnome DE). But I noticed that bash history is not saved once the terminal session is closed. Once I close a terminal session and open a new one, and run the command history it does not show anything other than the history command itself. Is there any way to solve this issue?

Thank you

Deepal
  • 1,696
  • 5
  • 19
  • 26

2 Answers2

85

It may be that your .bash_history doesn't belong to your user. Check the permissions of the file and if you're not the owner, change it:

sudo chown yourusername:yourusername ~/.bash_history
  • 4
    Worked for me. Curious how that happened though. – Klik Jan 08 '15 at 22:32
  • 3
    Same here. I also wonder why that happened? – Chris Apr 07 '15 at 20:49
  • 1
    Shouldn't you set the group as well, with sudo chown yourusername:yourusername ~/.bash_history? – The Guy with The Hat Nov 17 '17 at 21:35
  • Boombaa ! I take this moral from the story: "When such basic facility fails (and by default is ON), then most likely this is due to famous "permissions: problem." :) – Peter Nov 12 '18 at 15:46
  • This fixed the problem for me too. I strongly suspect that sudo -s was the problem but it does not happen every time. I note that -s leaves the home dir as the original. I was using that before my history vanished. – Russell Fulton May 13 '20 at 21:15
  • it happened to me once. There was blank permissions block. chmod 0644 .bash_history solved the problem "-rw-r--r--" – J. Doe Jul 04 '20 at 19:46
27

First type following command

shopt -s

to check weather histappend command is 'on' or 'off' If it is off then type following command to append history

history -a

If you want to save history with every new prompt run following command

export PROMPT_COMMAND='history -a'

For more information see this link

Chinmaya B
  • 7,082
  • histappend is 'on'. Can't figure out what's going on. :/ – Deepal Jul 18 '14 at 08:31
  • I think some pipe connecting to history file is broken ,because if from terminal history is append then only it can be case of broke pipe try reparing or reinstalling system if you can – Chinmaya B Jul 18 '14 at 08:52
  • 1
    histappend isn't even listed in the output of shopt -s even tho $HISTSIZE and $HISTFILESIZE are set properly. Idea? Thanks! – HCSF Oct 16 '19 at 00:29
  • its not working, when i have two terminals open and execute one command in the first terminal the command is not shown when i type history in the second terminal – Soerendip Aug 29 '25 at 11:46