Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

If I run history, I can see my latest executed commands.

But if I do tail -f $HISTFILE or tail -f ~/.bash_history, they do not get listed.

Does the file get locked, is there a temporary location or something similar?

share|improve this question
up vote 54 down vote accepted

Bash maintains the list of commands internally in memory while it's running. They are written into .bash_history on exit:

When an interactive shell exits, the last $HISTSIZE lines are copied from the history list to the file named by $HISTFILE

If you want to force the command history to be written out, you can use the history -a command, which will:

Append the new history lines (history lines entered since the beginning of the current Bash session) to the history file.

There is also a -w option:

Write out the current history to the history file.

which may suit you more depending on exactly how you use your history.

If you want to make sure that they're always written immediately, you can put that command into your PROMPT_COMMAND variable:

export PROMPT_COMMAND='history -a'
share|improve this answer

bash keeps it in working memory, bash can be configured to save it when bash closes or after each command, and to be loaded when bash starts or on request.

If you configure to save after each command, then consider the implications of having multiple bash running at same time. (command lines will be interleaved)

share|improve this answer
The start of you answer makes it sound as if the history is stored in a file called bash, or even in the bash exetable. I would write "It is stored by bash in memory, ..." – Anthon Jul 18 '14 at 9:08
Thanks I have now improved the answer. – richard Jul 18 '14 at 13:22
that's indeed better – Anthon Jul 18 '14 at 13:24

(Not an answer but I cannot add comments)

If you are checking .bash_history because you just want delete a specific command (e.g. containing a password in clear), you can directly delete the entry in memory by history -d <entry_id>.

For example, supposing an output like:

$ history
926  ll
927  cd ..
928  export --password=super_secret
929  ll

and you want purge the export line, you can simply achieve it by:

history -d 928
share|improve this answer

Commands are saved in memory (RAM) while your session is active. As soon as you close the shell, the commands list gets written to bash_history before shutdown.

Thus, you won't see history of current session in bash_history.

share|improve this answer
The history file is updated upon bash termination, which does not imply rebooting (especially in graphical environments where you can open and close terminals as you wish). – John WH Smith Jul 18 '14 at 12:50

protected by Community Nov 4 '15 at 13:53

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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