LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 12-12-2017, 11:24 PM   #1
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
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.
 
Old 12-13-2017, 12:52 AM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,781

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
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.
 
1 members found this post helpful.
Old 12-13-2017, 01:53 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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.
 
Old 12-14-2017, 04:22 PM   #4
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Original Poster
Rep: Reputation: 70
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.

Last edited by ArfaSmif; 12-14-2017 at 04:23 PM.
 
Old 12-15-2017, 02:57 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
you need to set HISTFILE, probably that is unset (by default). See post #3 and man page of bash.
 
Old 12-17-2017, 07:58 PM   #6
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Original Poster
Rep: Reputation: 70
Quote:
Originally Posted by pan64 View Post
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.
 
Old 12-18-2017, 03:13 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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..

Last edited by pan64; 12-18-2017 at 03:15 AM.
 
Old 12-18-2017, 12:50 PM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Arrow

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.
 
Old 12-18-2017, 01:52 PM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Quote:
Originally Posted by scasey View Post
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.

Last edited by pan64; 12-18-2017 at 01:55 PM.
 
Old 12-18-2017, 02:25 PM   #10
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by pan64 View Post
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?

Last edited by scasey; 12-18-2017 at 02:28 PM.
 
Old 12-18-2017, 02:48 PM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
no, it is not set at all, this is debian 9.
 
Old 12-18-2017, 03:02 PM   #12
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by pan64 View Post
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?
 
Old 12-18-2017, 03:18 PM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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
 
Old 12-18-2017, 04:42 PM   #14
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by pan64 View Post
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!
 
Old 12-19-2017, 12:27 AM   #15
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Original Poster
Rep: Reputation: 70
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.
 
  


Reply

Tags
bash, history, xfce4-terminal



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does BASH history command have no history when run in script ? lump_of_lard Programming 4 01-30-2015 09:45 AM
Bash history delete command from bash itself ashishag Linux - Software 6 05-02-2010 03:39 AM
need someone to look into this bash history bytez Linux - General 7 10-16-2006 10:26 PM
bash history jasonnth Linux - Software 7 08-08-2006 01:35 AM
Bash History ukndoit Linux - Security 2 10-16-2003 09:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration