Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It's my understanding that the history file is updated when the terminal exits. But sometimes my computer crashes out, and the terminal doesn't exit cleanly, and then I lose those commands from my history which is annoying. How can I make it flush immediately, so that the entries still go there even if my computer has a meltdown? At the moment I'm using this workaround, but I feel there should be a better way.

I'm using gnome-terminal on Ubuntu 12.10.

share|improve this question
up vote 8 down vote accepted

The answers in the link that you provided from the Super-Users site shouldn't necessarily be viewed as 'workarounds' to the history command's default behavior. The bash shell has some sane, out of the box, default behavior.

I would highly recommend reading How can I avoid losing any history lines? for an explanation of what these modifications to history are doing. Additionally, there are some reasonable concerns to be aware of in regards to why this is not the default behavior of the history command.

  • performance - Since you are saving every command from every window with history -a, the .bash_history file can grow quite large and require greater resources to load the bash shell. This can result in longer start up times(for your terminal sessions, not overall system startup, per se.).

  • organization - (from the above article) "the history commands of simultaneous interactive shells (for a given user) will be intertwined. Therefore the history is not a guaranteed sequential list of commands as they were executed in a single shell."

If you are concerned about further securing the bash shell and the .bash_history file through auditing, take a look at this article: How do I log history or "secure" bash against history removal?

On occasion (e.g. an unstable system, or power source), I have found the below commands useful.
You can add something similar to the following lines to your ~/.bashrc file:

PROMPT_COMMAND="history -a"

shopt -s histappend

Finally, exit your editor and source your .bashrc file: source ~/.bashrc

share|improve this answer

The simplest, working answer to the question "How to save terminal history manually?":

history -a

It may also be worth to consider switching to zsh, which has setopt inc_append_history ("save every command before it is executed").

And this question is relevant as well: Is it possible to make writing to .bash_history immediate?

share|improve this answer

To save bash history manually to a file:

history -w ~/history.txt
vim ~/history.txt

It exports the history to a file called history.txt. You can then view it using your favorite editor.

Answer copied from

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.