I don't think any shell does anything beyond whitespace munging when storing commands into the history. What *
expanded to is not recorded, and there doesn't seem to be an option to record it.
Note that it would not be possible to record in full generality anyway. For example, if you run a='foo* bar*'; rm $a
(rm $=~a
in zsh), the shell is unlikely to detect the wildcard match syntactically. And there's no option to log all glob expansions.
You could cobble something together with preexec
(or the bash equivalent). Untested first go (for zsh):
history_wildcards=()
precmd () {
emulate -LR zsh
local n tmp
history -1 | read n tmp
history_wildcards[$(($n+1))]=$=~1
}
This fills the history_wildcards
array to a string with the glob expansion of each word in the command line. Word splitting is performed in the naive way, so something like echo ' /**/* '
will expand /**/*
(i.e. traverse your disk). Don't expect anything useful from cd subdir && echo *
or anything so mind-bogglingly nontrivial.
If you plan beforehand, you can make zsh expand the wildcards before you submit the command. In the default configuration, just press Tab when your cursor is on the glob. If you've configured completion differently, try ^X *
(expand-word
).
This was a hack I used once on a shared account to track who did what:
PROMPT_COMMAND="history -a; sed -i '\$s/\$/ \#$NAME/' $HISTFILE; history -r; $PROMPT_COMMAND"
It writes the history
before the PROMPT
is displayed: in effect, after each command. You could modify this to use save the exit code in the history:
PROMPT_COMMAND="CODE=$?; history -a; sed -i '\$s/\$/ \#\$CODE/' $HISTFILE; history -r; $PROMPT_COMMAND"
This would leave entries in your history like:
$ history 2
1271 vim .bashrc #0
1272 history 2
(The last entry will be updated, after the history
command exits, so the exit code isn't visible in this output.)
This has the annoying effect that history in your prompt will have the comment:
$ # press up
$ history 2 #0
The comment doesn't affect execution or output usually, but makes editing a pain.
Now you can grep
your history to see which codes exited successfully:
history | grep '#0$'
# or grep '#[^0]*$' for unsuccessful commands.
Best Answer
Well, when
histappend
is not set, this does not mean that the history is wiped on each shell exit. Withouthistappend
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.