Can history files be unified in bash?
Solution 1:
There are two things you need to do:
- Insert the command
shopt -s histappend
in your.bashrc
. This will append to the history file instead of overwriting it. - Also in your
.bashrc
, insertPROMPT_COMMAND="$PROMPT_COMMAND;history -a; history -n"
and the history file will be re-written and re-read each time bash shows the prompt.
EDIT: Thanks to e-t172 for the history -n
trick
Solution 2:
Please don't use history -a; history -n
, it does not work as you expect and will leave you with many duplicate, out of order commands in your history. A solution that works generally as expected is the following:
# unified bash history
shopt -s histappend
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"
Using a newline instead of a semicolon is also a short way of dealing with the missing/duplicate semicolon problem with PROMPT_COMMAND.