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?
['https://unix.stackexchange.com/questions/145250', 'https://unix.stackexchange.com', 'https://unix.stackexchange.com/users/57143/']
While running, the history is kept only in memory (by default) if:

* set -o history (an `H` in `echo "$-"`) is set.
* HISTSIZE is not `0` **and**
* HISTIGNORE is not `*` (or some other very restrictive pattern).

If any of the above fail, no history is stored in memory and consequently no history could or will be written to disk.

History in memory is written to disk if:

* HISTFILESIZE is not 0 **and**
* HISTFILE is not unset.

But only when the shell exits or if the commands `history -a` (append) or `history -w` (write) are executed.

To trigger an immediate write to disk you can use the variable:

```
 PROMPT_COMMAND='history -a'

```

which will `append` the `new` history lines to the history file. These are history lines entered since the beginning of the current bash session, but not already appended to the history file.

Or:

```
 PROMPT_COMMAND='history -w'

```

To **overwrite** the history in the HISTFILE with the list from memory.

So, you can remove a command from the history in memory:

```
 $ history 5
  6359  ls
  6360  cd ..
  6361  comand --private-password='^%^&$@#)!@*'
  6362  top
  6363  set +o | less
 $ history -d 6361
 $ history 5
  6359  ls
  6360  cd ..
  6361  top
  6362  set +o | less
 $ history -w

```

And write it to disk with the last command:

```
 history -w    # with `shopt -u histappend` unset

```
The easiest way to find where your bash history is stored is with this:

`echo $HISTFILE`