There is a similar question here, but I want to achieve something different: I would like to share history between sessions, but without mixing the commands that were executed in different sessions together.

For example, let's say I have two shell sessions: A and B. I type some commands in A:

A$ ls ~
A$ ls /etc

And in B:

B$ git status
B$ git log

When I type history in a shell, I'd like to see all commands from this shell together, after commands from other shells - so that I can always use uparrrow to get last commands from the current shell. In other words, history in shell A should show

git status
git log
ls ~
ls /etc

And in shell B it should show

ls ~
ls /etc
git status
git log

How to do this?

up vote 3 down vote accepted

Seems there is no built-in solution for this, but it turned out implementing it manually is not that difficult. One has to store history of each session separately and recreate it on every prompt (it's not as slow as it may sound). Here's the core logic:

# on every prompt, save new history to dedicated file and recreate full history
# by reading all files, always keeping history from current session on top.
update_history () {
  history -a ${HISTFILE}.$$
  history -c
  history -r
  for f in `ls ${HISTFILE}.[0-9]* | grep -v "${HISTFILE}.$$\$"`; do
    history -r $f
  done
  history -r "${HISTFILE}.$$"
}
export PROMPT_COMMAND='update_history'

# merge session history into main history file on bash exit
merge_session_history () {
  cat ${HISTFILE}.$$ >> $HISTFILE
  rm ${HISTFILE}.$$
}
trap merge_session_history EXIT

See this gist for a full solution, including some safeguards and performance optimizations.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.