# Preserve bash history in multiple terminal windows

I continually have greater than one incurable open. Anywhere from 2 to 10, doing numerous little bits and also bobs. Currently allow's claim I reactivate and also open an additional set of terminals. Some bear in mind particular points, some neglect.

I desire a background that:

• Remembers every little thing from every incurable
• Is promptly obtainable from every incurable (eg if I ls in one, switch over to an additional already-running incurable and afterwards push up, ls turns up)
• Does not neglect command if there are rooms at the front of the command.

Anything I can do to make bash job extra like that?

666
2019-05-06 13:23:21
Source Share

You can modify your BASH motivate to run the "history - a" and also "history - r" that Muerr recommended:

savePS1=$PS1  (in instance you mess something up, which is virtually assured) PS1=$savePS1history -a;history -r


(note that these are back - ticks ; they'll run background - an and also background - r on every punctual. Given that they do not result any kind of message, your punctual will certainly be unmodified.

As soon as you've obtained your PS1 variable set up the means you desire, set it completely it in your ~/. bashrc documents.

PS1=$savePS1  I've done standard screening on this to make certain that it type of jobs, yet can not talk with any kind of side - results from running history -a;history -r on every punctual. 13 2022-06-08 07:54:36 Source If you require a bash or zsh background integrating remedy which additionally addresses the trouble listed below, after that see it at http://ptspts.blogspot.com/2011/03/how-to-automatically-synchronize-shell.html The trouble is the following: I have 2 shell windows An and also B. In covering window A, I run sleep 9999, and also (without awaiting the rest to coating) in covering window B, I intend to have the ability to see sleep 9999 in the bash background. The reason that most various other remedies below will not address this trouble is that they are creating their background adjustments to the the background documents making use of PROMPT_COMMAND or PS1, both of which are implementing far too late, just after the sleep 9999 command has actually ended up. 11 2022-06-08 07:54:29 Source So, this is all my background - relevant .bashrc point: export HISTCONTROL=ignoredups:erasedups # no duplicate entries export HISTSIZE=100000 # big big history export HISTFILESIZE=100000 # big big history shopt -s histappend # append to history, don't overwrite it # Save and reload the history after each command finishes export PROMPT_COMMAND="history -a; history -c; history -r;$PROMPT_COMMAND"


Tested with bash 3.2.17 on Mac OS X 10.5, bash 4.1.7 on 10.6.

292
2022-06-08 07:50:14
Source

You can make use of history -a to add the existing session is background to the histfile, after that make use of history -r on the various other terminals to read the histfile.

10
2022-06-08 07:50:12
Source

Here is my effort at Bash session background sharing. This will certainly enable background sharing in between bash sessions in such a way that the background counter does not get blended and also background development like !number will certainly function (with some restraints).

Making use of Bash variation 4.1.5 under Ubuntu 10.04 LTS (Lucid Lynx).

HISTSIZE=9000
HISTFILESIZE=$HISTSIZE HISTCONTROL=ignorespace:ignoredups _bash_history_sync() { builtin history -a #1 HISTFILESIZE=$HISTSIZE     #2
builtin history -c         #3
builtin history -r         #4
}

history() {                  #5
_bash_history_sync
builtin history "[email protected]"
}

PROMPT_COMMAND=_bash_history_sync


## Explanation:

1. Append the simply gotten in line to the $HISTFILE (default is .bash_history). This will certainly create $HISTFILE to expand by one line.

2. Establishing the unique variable $HISTFILESIZE to some value will certainly create Bash to trim $HISTFILE to be no more than $HISTFILESIZE lines by getting rid of the earliest access. 3. Clear the background of the running session. This will certainly lower the background counter by the quantity of $HISTSIZE.

4. Read the materials of $HISTFILE and also insert them in to the existing running session background. this will certainly raise the background counter by the quantity of lines in $HISTFILE. Keep in mind that the line matter of $HISTFILE is not always $HISTFILESIZE.

5. The history() function bypasses the builtin background to see to it that the background is synchronised prior to it is presented. This is essential for the background development by number (even more concerning this later).

## Extra description:

• Step 1 makes certain that the command from the existing running session obtains contacted the international background documents.

• Tip 4 makes certain that the commands from the various other sessions obtains read in to the existing session background.

• Due to the fact that action 4 will certainly raise the background counter, we require to lower the counter somehow. This is carried out in action 3.

• Symphonious 3 the background counter is lowered by $HISTSIZE. Symphonious 4 the background counter is elevated by the variety of lines in $HISTFILE. Symphonious 2 we see to it that the line matter of $HISTFILE is specifically $HISTSIZE (this suggests that $HISTFILESIZE has to coincide as $HISTSIZE).

## Concerning the restraints of the background development:

When making use of background development by number, you need to constantly seek out the number quickly prior to utilizing it. That suggests no bash punctual display screen in between seeking out the number and also utilizing it. That generally suggests no enter and also no ctrl+c.

Generally, as soon as you have greater than one Bash session, there is no warranty whatsoever that a background development by number will certainly preserve its value in between 2 Bash punctual display screens. Due to the fact that when PROMPT_COMMAND is implemented the background from all various other Bash sessions are incorporated in the background of the existing session. If any kind of various other bash session has a new command after that the background varieties of the existing session will certainly be various.

I locate this restraint practical. I need to look the number up every single time anyhow due to the fact that I can not bear in mind approximate background numbers.

Generally I make use of the background development by number similar to this

$history | grep something #note number$ !number


## reedit a history substitution line if it failed
shopt -s histreedit
## edit a recalled history line before executing
shopt -s histverify


## Strange pests:

Running the background command piped to anything will certainly result that command to be detailed in the background two times. As an example:

$history | head$ history | tail
$history | grep foo$ history | true
$history | false  All will certainly be detailed in the background two times. I have no suggestion why. ## Suggestions for renovations: • Modify the function _bash_history_sync() so it does not execute every single time. As an example it need to not execute after a CTRL+C on the punctual. I usually make use of CTRL+C to throw out a lengthy command line when I determine that I do not intend to execute that line. Occasionally I need to make use of CTRL+C to stop a Bash conclusion manuscript. • Commands from the existing session need to constantly be one of the most current in the background of the existing session. This will certainly additionally have the negative effects that an offered background number maintains its value for background access from this session. 134 2022-06-08 07:50:07 Source To do this, you'll require to add 2 lines to your ~/.bashrc: shopt -s histappend PROMPT_COMMAND="history -a;history -c;history -r;$PROMPT_COMMAND"


From man bash:

If the histappend covering alternative is made it possible for (see the summary of shopt under SHELL BUILTIN COMMANDS listed below), the lines are added to the background documents, or else the background documents mores than - created.

19
2022-06-05 17:51:51
Source

I can supply a solution for that last one : see to it the env variable HISTCONTROL does not define "ignorespace" (or "ignoreboth").

Yet I feel your discomfort with numerous simultaneous sessions. It merely isn't taken care of well in bash.

7
2019-05-08 17:30:56
Source

I'm not knowledgeable about any kind of means making use of bash. Yet it's one of one of the most preferred attributes of zsh.
Directly I favor zsh over bash so I advise attempting it.

Below's the component of my .zshrc that manage background :

SAVEHIST=10000 # Number of entries
HISTSIZE=10000
HISTFILE=~/.zsh/history # File
setopt APPEND_HISTORY # Don't erase history
setopt HIST_FIND_NO_DUPS # Don't show duplicates in search
setopt HIST_IGNORE_SPACE # Don't preserve spaces. You may want to turn it off
setopt NO_HIST_BEEP # Don't beep
setopt SHARE_HISTORY # Share history between session/terminals

56
2019-05-08 16:32:34
Source

Add the complying with to ~/. bashrc

# Avoid duplicates
export HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend

# After each command, append to the history file and reread it
export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND\$'\n'}history -a; history -c; history -r"

431
2019-05-08 15:47:56
Source