0

I'm new on Unix&Linux and wanted to comment on an already exhaustive approach to a different question. My rep isn't 50, yet (sad), so it won't let me. I tried this solution to this question: Preserve bash history in multiple terminal windows

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"

Unfortunately, this causes the result that while I have a growing (merged) history (desired), in new windows, I can no longer cycle through any of it using the 'up-arrow' (undesired). That is, I can only cycle through commands executed within the new window. Eg, if I open a new terminal and have 500 lines of history incoming, I execute 3 commands, I can only cycle through those 501-503 with the up-arrow...

I open a new terminal and do the following:

$ history #enter
  .
  .
  584 foo
  585 bar
  .
  .
  600 baz

$ history #enter

  1 history

Could somebody explain why this is so, and if there is a workaround? Thanks.

2 Answers2

1

Seems the solution can be navigated to from comment section in the link I posted: see Bash history: "ignoredups" and "erasedups" setting conflict with common history across sessions

Rather than:

export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

Use:

PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND"
0

The reason to have the history of all places where bash has an history (and writes to it) is that you are writing to the file (history -a) and all other bash shells are also writing to the same file.

Then, you are reading all commands from the file (history -r) to memory.

As a solution, you can write (new) commands to the file (history -a), but not read the resulting file. The list of commands in memory will belong to each instance of bash running. Each instance of bash will have a diferent list of commands. All commands will be written to the history file.

Use (in bashrc or similar):

PROMPT_COMMAND="history -a; $PROMPT_COMMAND"