Why isn't `history -a` working?

Solution 1:

I noticed the same odd behavior, checked permissions, and sure enough, both the "World" and "System" had explicit permissions set to read AND write my .bash_history file, but I (the owner) had no permissions to that file at all!

I just forced "World" to "No Access" and gave myself and system r/w access and voila, everything is working the way it should.

Solution 2:

For El Capitan (10.11.6) I ran into history -a not working - https://unix.stackexchange.com/q/473581/135943

Copying my self-answer from there:


It seems this is something Apple-specific. (I'm using Mac OS 10.11.6 El Capitan.)

My HISTFILE value is the immediate cause:

[512] $ echo $HISTFILE
/Users/Wildcard/.bash_sessions/8BC6B122-0D74-445B-B6A0-7D4D446598CB.historynew

But since I don't set that variable, where is it being set?

Aha, it's in /etc/bashrc_Apple_Terminal. And from the comments there, it looks like I only ran into this because I tried history -a by itself, without ever setting shopt -s histappend. They've coded it so that if you turn on histappend, or set the HISTTIMEFORMAT variable, they skip the session resume support code.

Here's the inline documentation for that section:

# Resume Support: Save/Restore Shell State
#
# Terminal assigns each terminal session a unique identifier and
# communicates it via the TERM_SESSION_ID environment variable so that
# programs running in a terminal can save/restore application-specific
# state when quitting and restarting Terminal with Resume enabled.
#
# The following code defines a shell save/restore mechanism. Users can
# add custom state by defining a shell_session_save_user_state function
# that writes restoration commands to the session file at exit. e.g.,
# to save a variable:
#
#   shell_session_save_user_state() { echo MY_VAR="'$MY_VAR'" >> "$SHELL_SESSION_FILE"; }
#
# During shell startup the session file is executed. Old files are
# periodically deleted.
#
# The default behavior arranges to save and restore the bash command
# history independently for each restored terminal session. It also
# merges commands into the global history for new sessions. Because
# of this it is recommended that you set HISTSIZE and HISTFILESIZE to
# larger values.
#
# You may disable this behavior and share a single history by setting
# SHELL_SESSION_HISTORY to 0. There are some common user customizations
# that arrange to share new commands among running shells by
# manipulating the history at each prompt, and they typically include
# 'shopt -s histappend'; therefore, if the histappend shell option is
# enabled, per-session history is disabled by default. You may
# explicitly enable it by setting SHELL_SESSION_HISTORY to 1.
#
# The implementation of per-session command histories in combination
# with a shared global command history is incompatible with the
# HISTTIMEFORMAT variable--the timestamps are applied inconsistently
# to different parts of the history; therefore, if HISTTIMEFORMAT is
# defined, per-session history is disabled by default.
#
# Note that this uses PROMPT_COMMAND to enable per-session history
# the first time for each new session. If you customize PROMPT_COMMAND
# be sure to include the previous value. e.g.,
#
#   PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }your_code_here"
#
# Otherwise, the per-session history won't take effect until the first
# restore.
#
# The save/restore mechanism is disabled if the following file exists:
#
#   ~/.bash_sessions_disable