# save history for multiple terminals # create a folder to store the history if [ ! -d "${HOME}/.bash_history_log" ]; then mkdir "${HOME}/.bash_history_log" fi # Add the tty to the history file # Each terminal's history will be stored in # its own file if [ ! -z "${TMUX_PANE}" ]; then HISTSUFFIX=$(tmux display-message -p '#S#W' | sha256sum | cut -d' ' -f1) else HISTSUFFIX=`tty | sed 's/\///g;s/^dev//g'` fi HISTFILE="$HOME/.bash_history_log/bash_history_$HISTSUFFIX" # Time stamp for the history HISTTIMEFORMAT="%y-%m-%d %H:%M:%S " # don't put duplicate lines or lines starting with space in the history. HISTCONTROL=ignoredups:ignorespace # for setting history length see HISTSIZE and HISTFILESIZE in bash(1) HISTSIZE=1000 HISTFILESIZE=5000 # append to the history file, don't overwrite it shopt -s histappend # Add to the history everytime the prompt is shown PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007";history -a' # Search history function searchhistory() { grep -ri "${1}" ~/.bash_history_log/* } alias shistory=searchhistory # Thanks for that: https://dev.to/mrrcollins/saving-bash-history-across-terminals-b80