The Computer Oracle

Can history files be unified in bash?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Track title: CC M Beethoven - Piano Sonata No 3 in C 3

--

Chapters
00:00 Can History Files Be Unified In Bash?
00:29 Accepted Answer Score 43
01:07 Answer 2 Score 17
01:37 Answer 3 Score 0
01:56 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.