How To: Multiple history files for Bash

kghose

New Member
Joined
Mar 26, 2024
Messages
20
Reaction score
12
Credits
163
Detailed post: https://kghose.github.io/linux/bash-history.html

If you like to keep multiple terminals open for different purposes and you wanted a way to keep the command history for each of them, and keep them separate:

Here is the relevant part of my ~/.bashrc

Code:
# Create a directory for history files if needed.
HISTDIR="${HOME}/.history.d"
mkdir -p ${HISTDIR}

# Create a separate history file for each tty.
export HISTFILE="${HISTDIR}/tty`tty|cut -c10-`.hist"
# Append to history rather than overwrite it
shopt -s histappend

# These are personal preferences,
# not needed for multiple history files
export HISTCONTROL=ignoredups:erasedups
export HISTSIZE=1000
export HISTFILESIZE=1000

# A creature comfort to remind ourselves that
# we're saving to non default history files
echo "History file: ${HISTFILE}"

With this setup, whenever we open a terminal it either creates or reuses a history file based on its tty identity allowing us multiple histories for different terminals that persist across us closing and opening terminals.
 



Members online

No members online now.

Latest posts

Top