The Computer Oracle

Can history files be unified in bash?

-------------------------------------------------------------------------------
Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn
-------------------------------------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: A Thousand Exotic Places Looping v001

--

Chapters
00:00 Question
00:34 Accepted answer (Score 43)
01:16 Answer 2 (Score 17)
01:50 Answer 3 (Score 0)
02:13 Thank you

--

Full question
https://superuser.com/questions/37576/ca...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #bash

#avk47



ACCEPTED ANSWER

Score 43


There are two things you need to do:

  1. Insert the command shopt -s histappend in your .bashrc. This will append to the history file instead of overwriting it.
  2. Also in your .bashrc, insert PROMPT_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




ANSWER 2

Score 17


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.




ANSWER 3

Score 0


None of the solution here worked for me, but what did work was just using:

cat ~/.bash_history | grep something

instead of

history | grep something

Kudos to this SO answer.