Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 :-
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.
In that case (probably) all the terminals have their own history file. (bash can be configured that way).
or see man bash
Code:
HISTFILE
The name of the file in which command history is saved (see HISTORY below). The default value is
~/.bash_history. If unset, the command history is not saved when a shell exits.
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.
If not set specifically HISTFILE defaults to ~/.bash_history. So in my case the history file is in fact ~/.bash_history and always saves the command history.
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
/home/scasey/.bash_history
Yes, each terminal has its own history file, in memory.
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.
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]$
In that case history -a is not required - or will cause doubled history entries. Otherwise it looks like it is saved only when the shell exited, so you cannot see the history of any other running bash.
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.
In that case history -a is not required - or will cause doubled history entries. Otherwise it looks like it is saved only when the shell exited, so you cannot see the history of any other running bash.
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.
Agreed, history -a is not required unless you want to save the history without closing the session before opening a new session.
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
or
Code:
tail ~/.bash_history
Also, yes, I don't find anything in the bash configurations to set HISTFILE, but, as you point out in #3 man bash says "The default value is ~/.bash_history."...maybe it's compiled into bash?
Just curious, is it not preset in the distros you're using?
Interesting...yet your man bash says it has a default (as does mine...it has exactly that same line for HISTFILE). Most curious.
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
to write to the history file without closing the session.
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
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
shopt -s histappend
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
shopt -s histappend
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.