The problem actually boils down to the different behavior of login and non-login shells. I had set the variables that control history in my ~/.bahsrc
. This file is not read when one starts a login shell, it's only read by interactive, non-login shells (from man bash
):
When bash is invoked as an interactive login shell, or as a
non-interactive shell with the --login
option, it first reads
and executes commands from the file /etc/profile
, if that file
exists. After reading that file, it looks for ~/.bash_profile,
~/.bash_login
, and ~/.profile
, in that order, and reads and
executes commands from the first one that exists and is
readable. The --noprofile
option may be used when the shell is
started to inhibit this behavior.
[. . . ]
When an interactive shell that is not a login shell is started,
bash reads and executes commands from ~/.bashrc, if that file
exists. This may be inhibited by using the --norc option. The
--rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.
Therefore, each time I logged in, or dropped to a tty, or used ssh, the .history
file was getting truncated because I had not set it to the right size in ~/.profile
as well. I finally realized this and simply set the variables in ~/.profile
where they belong, instead of ~/.bashrc
So, the reason my ~/.history
was getting truncated was because I had only set the HISTORY variables in a file read by interactive, non-login shells and therefore every time I ran a different type of shell the variables would be ignored and the file would be cut accordingly.
Best Answer
Bash History
Any new commands that have been issued in the active terminal can be appended to the
.bash_history
file with the following command:The only tricky concept to understand is that each terminal has its own bash history list (loaded from the
.bash_history
file when you open the terminal)If you want to pull any new history that's been written by other terminals during the lifetime of this active terminal, you can append the contents of the
.bash_history
file to the active bash history listThis will clear the current history list so we don't get a repeated list, and append the history file to the (now empty) list.
Solution
You can use the bash variable
PROMPT_COMMAND
to issue a command with each new prompt (every time you press enter in the terminal)This will record each command to the history file as it is issued.
Result
Now any new terminal you open will have the history of other terminals without having to
exit
those other terminals. This is my preferred workflow.More Precision
Let's say (for some reason) you have two terminals that you're using simultaneously and you want the history to reflect between both for each new command.
The main drawback here is that you may need to press enter to re-run the PROMPT_COMMAND in order to get the latest history from the opposite terminal.
You can see why this more precise option is probably overkill, but it works for that use case.