Hacker News new | past | comments | ask | show | jobs | submit login

I like keeping _all_ my shell history, with timestamps. Even the fuckups. It's come in handy a few times now

* Looking back at my old process for onboarding into some new technology. I can easily see how my technique evolved as I optimized for more correctness (e.g. cp -r vs cp -rp vs rsync with whatever flags does everything I want, I know I want the most recent incantation)

* Figure out the exact process I did for a backup or other movement of data when I want to ensure I have copied X, Y, and Z. Sometimes my shell history has saved me where my notes have failed.






I have most of my shell history back to 2005(?). Each terminal gets its own new history file.

99% of the time, I never look at it, but when I do need to look at it, it has been great. My boss once asked me: "What args and screening file did we use when we made that one-off DB 4 months ago?" Was able to check and confirm it was correct. Or for personal use: "Where did I move that folder of pictures?"


I opted for a single history across all sessions on any given host: On my main machine, the first of 54,434 entries is timestamped 2020-08-22:12:39, while on the machine on which I do most development at the moment (it varies from product to product and release to release), the first of 34,771 entries is timestamped 2023-05-08:11:34.

For the curious, the salient .bashrc bits are:

  function _setAndReloadHistory {
      builtin history -a
      builtin history -c
      builtin history -r
  }
  
  # preserve shell history
  set -o history
  # preserve multiline commands...
  shopt -s cmdhist
  # preserve multiline command as literally as possible
  shopt -s lithist
  # reedit failed history substitutions
  shopt -s histreedit
  # enforce careful mode... we'll see how this goes
  shopt -s histverify
  
  # timestamp all history entries
  HISTTIMEFORMAT='%Y-%m-%d:%H:%M '
  
  # not the default, we like to be explicit that we are not using defaults
  HISTFILE=~/.bash_eternal_history
  # preserve all history, forever
  HISTSIZE=-1
  # preserve all history, forever, on disk
  HISTFILESIZE=-1
  # record only one instance of a command repeated after itself
  HISTCONTROL=ignoredups
  
  # preserve history across/between shell sessions...
  # ...when the shell exits...
  shopt -s histappend
  # ...and after every command...
  PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; } _setAndReloadHistory"
EDIT: Remembered just after submitting that since I am on MacOS, I ran the command

  touch ~/.bash_sessions_disable
back on August 22nd, 2020, to prevent Terminal from saving per-session information. I've never cleaned out ~/.bash_sessions, suppose I should, but it hasn't been updated since that day.

You might also want to add one improvement:

Write commands to history files before running the commands instead of after the commands finish:

  trap 'history -a' DEBUG
Or if you already have a DEBUG trap:

  trap "history -a ; $(trap -p DEBUG | cut -d "'" -f 2)" DEBUG



Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: