How to sync terminal session command history in bash?

561

Solution 1

Add this line to .bashrc:

export PROMPT_COMMAND="history -a; history -n"

Open new terminal and check.

Explanation

  • history -a appends new history lines to history file.
  • history -n tells bash to read lines that is not read from history file to current history list of session.
  • PROMPT_COMMAND: contents of this variable is run as regular command before bash show prompt. So every time after you execute a command, history -a; history -n is executed, and your bash history is synced.

Solution 2

While I like being able to share history between terminals, especially new terminals. I would not want to share each and every command as it happens, as one window is often doing a specific task, separate form other windows. I would have them merge on shell exit, or when I request.

For a long time I looked for a way to merge bash history (with timestamps), and nothing seemed acceptable to me...

Finally I just 'bit the bullet' and DIY'ed a script to merge, the on-disk ".bash_history" with the in-memory shell 'history'. Preserving timestamp ordering, and command order within those timestamps.

Now when I source this (you could make it a alias or a function as you like), I use the alias 'hc'. My current shell session is merged between disk and memory, so history is updated from other previous merges, WHEN I WANT (or on logout from that shell via ".bash_logout").

Optionally you can remove unique commands (even if multi-line), and/or removing (cleaning out) simple and/or sensitive commands, according to defined perl RE's. Adjust to suit!

This is the result... https://antofthy.gitlab.io/software/history_merge.bash.txt

Enjoy.

Share:
561

Related videos on Youtube

Paul Riker
Author by

Paul Riker

Updated on September 18, 2022

Comments

  • Paul Riker
    Paul Riker over 1 year

    I have an SSRS report with two datasets: Projects (ID, Project, Status) and Tasks (ID, Title, AssignedTo, Project). I have a tablix for Tasks that references values in the Projects dataset using the Lookup function.

    The report runs fine when I run it on demand. When it runs as a subscription it logs the error "An item with the same key has already been added". When I researched this error most articles referenced having different field names, but when I try changing the field names in the dataset for Projects I get the error "The field expression for the dataset 'Projects' refers to the field 'ID'. Report item expressions can only refer to fields within the current dataset scope or, if inside an aggregate, the specified dataset scope"

    I just want a report that joins two lists and can be produced as a subscription. Any ideas?

  • n611x007
    n611x007 almost 10 years
    thanks! I don't know why did I get permission denied before but with new terminals now this seems to work as expected! I guess I should normally be able to use history -a in a terminal with unmodified bashrc too.
  • Fabian
    Fabian almost 10 years
    Also you might already have a prompt command, in this case it is better to use export PROMPT_COMMAND="${PROMPT_COMMAND};history -a; history -n"
  • Boris Däppen
    Boris Däppen about 7 years
    If ${PROMPT_COMMAND} is empty this gives an error because of the leading ;
  • TML
    TML almost 6 years
    @BorisDäppen is right, but this is easily remedied export PROMPT_COMMAND="${PROMPT_COMMAND}${PROMPT_COMMAND:+;}history -a; history -n"
  • x-yuri
    x-yuri over 3 years
    I would advise against messing with history after every command. I'm not sure about you, but when I press Up, I expect to see the command that was executed in this particular shell. The same goes for sudo !!. See this answer.
  • Sam Watkins
    Sam Watkins about 2 years
    +1 good point about not wanting to load history from other terminals constantly. I decided to go with history -a in the PROMPT_COMMAND, and run (an alias for) history -n when I do want to load history from other shells. Your scripts looks good but it's over-powered for my current needs.