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/']
Bash maintains the list of commands internally in memory while it's running. They are written into [`.bash_history` on exit](https://www.gnu.org/software/bash/manual/bashref.html#Bash-History-Facilities):

> 
> 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`](https://www.gnu.org/software/bash/manual/bashref.html#index-history) 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'

```
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`.