bash history settings
Hello all,
If I remember correctly, when I was a young fellow using sysv style *nixes, all shell command history would append to the appropriate file (eg. ~/.bash_history, I used to use "sh" and now I use "bash") and be able to be seen and used immediately in any open terminal. These days I run Fedora 27, bash-4.4.12-12.fc27.x86_64, and xfce4-terminal-0.8.6-3.fc27.x86_64. I have set the xfce4-terminal as "Run command as login shell" as opposed to an interactive shell. My ~/.bashrc file contains the following :- export HISTTIMEFORMAT="%F %T " export HISTCONTROL=ignoreboth:erasedups export HISTSIZE=-1 export HISTFILESIZE=-1 So my problem is that when I run a set of commands in one terminal I can use command history and reuse those commands. If I however start a new terminal I do not see these commands in the bash history but rather get a set of commands that are up to, but not including the commands that were used in the previous terminal. I thought that running a terminal as a login shell would reread the bash history file every time a new terminal was created and I would be able to see all commands previously entered. Now am I remembering correctly that this it how it used to work (or not), and if so can I do the same now? I hope I have explained myself clearly enough for someone to follow what I have said and give me advice. Thanks. |
The history (in memory) is saved to .bash_history when the shell exits.
To save without exiting do Code:
history -a |
In that case (probably) all the terminals have their own history file. (bash can be configured that way).
or see man bash Code:
HISTFILE |
Thank you all for your replies.
It seems that the way HISTORY works in the shell is that if you use "history -a" as MadeInGermany has suggested and then open a new shell you will be able to see all the commands previously used in this new shell. So the only way to see ALL commands used is to use "history -a" and then open a NEW shell. The complete command history will be available. Unfortunately this is not what I want, and I don't think it is even possible without some sort of scripting trickery. |
you need to set HISTFILE, probably that is unset (by default). See post #3 and man page of bash.
|
Quote:
|
you still need to read the man page, see also #3:
If unset, the command history is not saved when a shell exits. (therefore it is just local to the current shell) You do not need to use history -a, but specify a histfile and all will be done automatically.. |
pan64: in CentOS 7 at least, the default for HISTFILE is ~/.bash_history, so it is set by default, and the history is saved, by default.
Code:
$ echo $HISTFILE ArfaSmif: In my experience the history behavior has always been as you're seeing it: The current session history is only saved to disk at the end of the session, so opening a new session will not see that history unless you first execute a history -a command. I often run two sessions for the same user at the same time, and have observed that the second session doesn't know the history of the first session, and vice versa. But, when closing the sessions, each saves its history to disk, so the next time I log in, both histories are available. |
Quote:
by the way, I don't know how it is going on CentOS, but if it was set by default probably it was set by something else, not bash itself. So it is not really default of bash, but preset on CentOS. |
Quote:
Note that executing history -a appears to cause a write (append) to HISTFILE, a flush of the in-memory history and a re-read of the HISTFILE, so doubling does not occur. [Just confirmed that in practice, but don't see any documentation of it] One can compare the in-memory history to the file by doing Code:
cat ~/.bash_history Code:
tail ~/.bash_history Just curious, is it not preset in the distros you're using? |
no, it is not set at all, this is debian 9.
|
Quote:
We've gotten a bit off track from the OPs question, so to repeat what MadeInGermany posted in #2: On a RedHat clone OS (Fedora, CentOS), at least, using bash, the history file is only written to when the session is closed. Use Code:
history -a This will make the entire history available to any new sessions. That is how it has "always" worked AFAIK. That was the behavior on Solaris when I used bash there, too. How are you opening the new session? Why is it "not possible" to Code:
history -a |
just a remark: if it was the default you need not set it anywhere, bash will use the default by default. But won't save...
But actually there is a setting in ~/.bashrc: Code:
# append to the history file, don't overwrite it |
Quote:
|
scasey: I think you're right that the command history has always worked like this. My memory is not what it used to be.
If you echo $BASHOPTS you will probably find that "histappend" is already part of you shell options. It _is_ on my Fedora 27 system. Looking at my first post you will notice that I have HISTCONTROL=ignoreboth:erasedups set. This means that the history ignores all spaces and ignores all duplicate commands and also only writes unique commands to the command history file. I also have HISTSIZE=-1 and HISTFILESIZE=-1 which means that every command is saved in the history file and the history file is never truncated and keeps all history from day one. |
All times are GMT -5. The time now is 09:29 AM. |