Skip to content

Instantly share code, notes, and snippets.

@xhd2015
Created July 30, 2025 12:07
Show Gist options
  • Select an option

  • Save xhd2015/cbf9024d76b5945078d002b545f16e0b to your computer and use it in GitHub Desktop.

Select an option

Save xhd2015/cbf9024d76b5945078d002b545f16e0b to your computer and use it in GitHub Desktop.
Bash history recording and logging
After asked grok3 lots of options, the only one that works for me:
```sh
# log and append history while preserving $_
append_log_history() {
# Save the current $_
local saved_underscore="$_"
echo -n '' > /tmp/history$$
HISTFILE=/tmp/history$$ history -a
cat /tmp/history$$ >> "$BASH_HISTORY_BACKUP_FILE"
cat /tmp/history$$ >> "$HISTFILE"
history -n
# Restore $_
export _="$saved_underscore"
}
shopt -s histappend
PROMPT_COMMAND="append_log_history; $PROMPT_COMMAND"
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment