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?
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
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
export PROMPT_COMMAND='history -a'
(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
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)
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
While running, the history is kept only in memory (by default) if:
echo "$-") is set.
*(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:
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:
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.
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
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?