Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

3 Answers 3

up vote 25 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
1  
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 at 9:08
    
Thanks I have now improved the answer. –  richard Jul 18 at 13:22
    
that's indeed better –  Anthon Jul 18 at 13:24

Commands are saved in memory (RAM) until your session is active. As soon as you reboot, 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
4  
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 at 12:50

Your Answer

 
discard

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.