LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash history settings (https://www.linuxquestions.org/questions/linux-general-1/bash-history-settings-4175619483/)

ArfaSmif 12-12-2017 11:24 PM

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.

MadeInGermany 12-13-2017 12:52 AM

The history (in memory) is saved to .bash_history when the shell exits.
To save without exiting do
Code:

history -a
The history is read from .bash_history when a new bash starts.

pan64 12-13-2017 01:53 AM

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.


ArfaSmif 12-14-2017 04:22 PM

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.

pan64 12-15-2017 02:57 AM

you need to set HISTFILE, probably that is unset (by default). See post #3 and man page of bash.

ArfaSmif 12-17-2017 07:58 PM

Quote:

Originally Posted by pan64 (Post 5793419)
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.

pan64 12-18-2017 03:13 AM

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..

scasey 12-18-2017 12:50 PM

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 12-18-2017 01:52 PM

Quote:

Originally Posted by scasey (Post 5794663)
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.

scasey 12-18-2017 02:25 PM

Quote:

Originally Posted by pan64 (Post 5794684)
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?

pan64 12-18-2017 02:48 PM

no, it is not set at all, this is debian 9.

scasey 12-18-2017 03:02 PM

Quote:

Originally Posted by pan64 (Post 5794705)
no, it is not set at all, this is debian 9.

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
Code:

history -a
before opening the new session?

pan64 12-18-2017 03:18 PM

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 12-18-2017 04:42 PM

Quote:

Originally Posted by pan64 (Post 5794720)
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


So that causes the save on shutdown, right? Cool!

ArfaSmif 12-19-2017 12:27 AM

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.