5 ms·
It seems like Bash history was made for a time that is long gone, and has not been fixed to work in a modern environment. I have tons of shells open, and I cann
by TorKlingberg 10y ago
It seems like Bash history was made for a time that is long gone, and has not been fixed to work in a modern environment. I have tons of shells open, and I cannot promise to close them carefully and sequentially.
Instead I found this to add to my .bashrc:
pcmd='if [ "$(id -u)" -ne 0 ]; then echo "$(pwd) $(history 1)" >> ~/.logs/bash-history-$(date "+%Y-%m-%d").log; fi'
PROMPT_COMMAND="$PROMPT_COMMAND;$pcmd"
It keeps all commands from all open shells in dated log files that I can grep through when Ctrl+R fails me.
Just be aware that if someone hacks into your PC they can see every command you have ever run, including any passwords accidentally pasted.
- milosivanovic 10y agoYou can append to .bash_history immediately, and thus make the items available in any other open shells immediately as well. This is supported natively. Search for `shopt -s histappend`.
- TorKlingberg 10y agoI want up arrow to give the last command from that shell though. I'd like Ctrl+R to search all the full history, but I don't think bash can do that combination. Also I just don't trust bash to actually keep my history, no matter what I do with HISTSIZE and HISTFILESIZE.
- gknoy 10y ago`histappend` probaby works the way you want it to. I have about a half dozen terminals open. I want to have the current shell's history seem consistent within any shell, but I want ^R in any NEW terminal to include all the commands entered in any terminal. shopt -s histappend HISTCONTROL=ignoredups:ignorespace # append to bash history Right Now export PROMPT_COMMAND="history -a" I share your distrust, though -- I've frequently lost my bash history for $REASONS (which another comment here seems to have answered :)) -- but the convenience of not having my shell history clobbered by which-shell-I-close-first is _amazing_.
- marklgr 10y ago'histappend' just makes the shell append its history list (from current session) to the history file, instead of overwriting it. This still occurs at exit time, not immediately. Besides, since you load the HISTFILE at startup, you'll append your whole history (file+session) to the HISTFILE each time you exit a shell, so either it will grow very large quickly, or you set HISTFILESIZE to limit it but as a consequence you'll lose entries (and unfortunately not the oldest). The option "only append the session entries typed in this session", while still using the whole history (file+session) for searching, is missing, as far as I know.
- chronial 10y agoFish shell has that problem fixed. History is always saved correctly and if you want access to the history of your other open shells, you can run history --merge
- TorKlingberg 10y agoYes, I should try fish or zsh. I'm just afraid I will spend a lot of time debugging incompatibilities. At least my current toolset doesn't involve source:ing stuff, so it should be ok.
- etatoby 10y agoYou just have to configure it to do so: shopt -s histappend HISTCONTROL=ignoredups:erasedups PROMPT_COMMAND='history -a;history -n' See more examples and discussions here: http://unix.stackexchange.com/questions/1288/preserve-bash-history-in-multiple-terminal-windows http://unix.stackexchange.com/questions/1288/preserve-bash-h...