Shared command history makes commands typed in one interactive Bash terminal appear in another terminal before either shell exits. It helps operators who keep separate tabs for editing, testing, and monitoring reuse the same recent commands without waiting for logout-time history writes.

Bash normally writes interactive history to HISTFILE when each shell exits. The histappend option keeps exit-time saves from overwriting another terminal's history, while a PROMPT_COMMAND hook can run history -a and history -n before each prompt to append this shell's new commands and import commands appended by other shells.

The setup belongs in ~/.bashrc for interactive non-login shells and should preserve any existing prompt hook. Use a hook that handles both older scalar PROMPT_COMMAND values and Bash's array form, and keep secret values out of typed commands because shared history can spread accidental entries across open terminals quickly.

Steps to enable shared command history in Bash:

  1. Back up the interactive Bash startup file.
    $ cp ~/.bashrc ~/.bashrc.bak

    If ~/.bashrc does not exist yet, create it with touch ~/.bashrc before adding the shared-history settings.

  2. Open ~/.bashrc in a text editor.
    $ nano ~/.bashrc
  3. Add the shared-history settings near the end of ~/.bashrc.
    ~/.bashrc
    HISTFILE=$HOME/.bash_history
    HISTSIZE=10000
    HISTFILESIZE=20000
    shopt -s histappend
     
    __bash_shared_history() {
        history -a
        history -n
    }
     
    __bash_prompt_command_decl=$(declare -p PROMPT_COMMAND 2>/dev/null || printf "")
     
    case "$__bash_prompt_command_decl" in
    declare\ -a*)
        __bash_shared_history_found=0
        for __bash_prompt_command in "${PROMPT_COMMAND[@]}"; do
            [ "$__bash_prompt_command" = "__bash_shared_history" ] && __bash_shared_history_found=1
        done
        (( __bash_shared_history_found )) || PROMPT_COMMAND+=(__bash_shared_history)
        ;;
    *)
        case ";${PROMPT_COMMAND:-};" in
            *";__bash_shared_history;"*) ;;
            *) PROMPT_COMMAND="__bash_shared_history${PROMPT_COMMAND:+; $PROMPT_COMMAND}" ;;
        esac
        ;;
    esac
     
    unset __bash_prompt_command_decl __bash_prompt_command __bash_shared_history_found

    Place this block after other lines that assign PROMPT_COMMAND. The array branch keeps Bash prompt-command arrays intact, while the scalar branch preserves older prompt hooks stored as one command string.

  4. Check ~/.bashrc for Bash syntax errors.
    $ bash -n ~/.bashrc

    No output means Bash did not find a parse error.

  5. Reload ~/.bashrc in the current terminal.
    $ source ~/.bashrc
  6. Open a second terminal that loads the updated ~/.bashrc.
  7. Run a unique command in the second terminal.
    $ echo shared-history-check
    shared-history-check
  8. Return to the first terminal and list recent history after the next prompt appears.
    $ history 5
     1022  echo shared-history-check
     1023  history 5

    History numbers vary by shell. The echo shared-history-check line should appear before either terminal exits.