The code for this is in /etc/{bashrc,zshrc}_Apple_Terminal
, in the default configuration it includesensures that shell history is preserved even if you have several tabs open in Terminal.
Both files include details about configuration options and also lists the wayexplain how to disable it as a whole.
To disable in zsh
# The save/restore mechanism as a whole can be disabled by setting an
# environment variable (typically in `${ZDOTDIR:-$HOME}/.zshenv`):
#
# SHELL_SESSIONS_DISABLE=1
To disable in bash
# The save/restore mechanism is disabled if the following file exists:
#
# ~/.bash_sessions_disable