Understanding `history` for bash

Yesterday I ssh

-d into my RemoteFS and ran some commands.

They don't appear in history today. Disappointing as it took a while to look at the commands I used.

I get this problem a lot. I suspect that using the same login for multiple concurrent terminal sessions may result in a separate history for each. And changing the user (like being promoted to superuser) opens up another set of stories.

Can anyone explain scientifically the life cycle of a story? When is a new one created? How to access / view all existing ones? And under what circumstances is history destroyed? Do they ever team up?

+3


source to share


1 answer


Depends on variable settings, but by default there is only one history file per user, not a terminal session.

History is currently stored in a memory buffer and only written to the history file when the buffer is full or complete. Therefore, multiple terminal sessions under the same user can overwrite each other's history. The history system is not suitable for multiple sessions under the same user ID.

If you want to keep separate sessions, change the variable HISTFILE

.

It might sound neat:

HISTFILE="$HOME/.bash_history$$"

      

where $$

sets the current PID. While this gives each terminal session its own history, it quickly becomes a maintenance nightmare in which all those history files are moved around.

There are other variables that control history, see man bash

for a description. You also can:

set | grep '^HIST'

      



which can be instructive.

Don't try to edit the history file with a text editor. It is a binary file (contains non-text fields) and can be easily destroyed.

When is a new one created? The name of the history file is used the first time.

How to access / view all existing ones? Depends on what name you gave them.

And under what circumstances is history destroyed? When HISTSIZE

exceeded (default 500 lines). Only strings are saved HISTSIZE

. Remember that the file itself is only overwritten when the buffer in memory is full or when you log off. However, we have an option histappend

:

shopt -s histappend

      

which will add the session and not overwrite it. Be careful with this, you can end up with a huge history file.

Are they ever going to? Not unless you are writing a script, or install histappend

.

+1


source







All Articles