62

After finding out what this shopt -s histappend means, it seems a very sane setting, and I'm surprised that it isn't default. Why would anyone want to wipe their history on each shell exit?

tshepang
  • 65,642
  • aside from the misunderstanding of what this option does - corrected by @maxscchlepzig - "default" has at least two sources: bash 'pure' or a distro-supplied .bashrc. for the former, @Gilles is probably correct. in the latter sense, -s histappend is default on, e.g., Debian, since 2008: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=452459 – underscore_d Nov 16 '15 at 23:00
  • 3
    Well, unix isn't exactly famous for sane defaults? I've come to expect the most unfriendly defaults by default :P (minor exaggerated) – olejorgenb Oct 20 '16 at 08:27

2 Answers2

61

Well, when histappend is not set, this does not mean that the history is wiped on each shell exit. Without histappend bash reads the histfile on startup into memory - during operation new entries are added - and on shell exit the last HISTSIZE lines are written to the history file without appending, i.e. replacing the previous content.

For example, if the histfile contains 400 entries, during bash runtime 10 new entries are added - histsize is set to 500, then the new histfile contains 410 entries.

This behavior is only problematic if you use more bash instances in parallel. In that case, the history file only contains the contents of the last exiting shell.

Independent of this: There are some people who want to wipe their history on shell exit because of privacy reasons.

tshepang
  • 65,642
maxschlepzig
  • 57,532
  • Also, for security reasons, it can be a good idea to not save history for the root user. This feature allows root to have a shell history without it ever being saved to disk. – Mei Feb 04 '12 at 00:50
  • 1
    +1 for "does not mean that the history is wiped on each shell exit". This is explained perfectly well elsewhere - but perhaps not succinctly enough to reach some readers, especially in the context of the option's name (OK, I might have been one of those readers for awhile). @Mei, how does either permutation of histappend have any relevance to whether root's history reaches a disk? Again, it only affects what is written, not if. – underscore_d Nov 16 '15 at 22:54
  • 9
    does anyone run less than 2 bash instances? histappend as well as HISTCONTROL=ignoredups:erasedups:ignorespace seems like a good default for most people. – dashesy May 25 '16 at 22:22
  • 1
    Is histappend effective meaningless if I have HISTFILESIZE= and HISTSIZE= for infinite history? – Marc.2377 Jun 17 '19 at 01:32
  • 2
    @Marc.2377 No, without histappend new history from concurrently running shells would then be lost and only the entries of the last exiting shell would be saved. Btw, I don't know if HISTSIZE= has the same effect as a negative HISTSIZE. – maxschlepzig Jun 17 '19 at 18:26
  • 3
    @dashesy I personally don't like to ignore/erase duplicate entries. I have HISTTIMEFORMAT enabled, and I like to be able to go back and see when I issued a command... and if I issued it multiple times, I like to know when each one was issued. It's a personal preference, to which their own. ;) – msb Nov 20 '19 at 19:14
  • See also https://unix.stackexchange.com/questions/226214/why-does-history-not-overwrite-but-append-when-histappend-is-set-to-off-in-bash/428208#428208 – Matthias Braun May 27 '23 at 19:42
12

For historical compatibility, I guess. The histappend option didn't exist until bash 2.0.