4 ms·
TFA mentions shared history across sessions as a ZSH feature, but it is available in bash as well. For those who're staying with bash, stick this in your .bash
by keyist 17y ago
TFA mentions shared history across sessions as a ZSH feature, but it is available in bash as well. For those who're staying with bash, stick this in your .bashrc:
shopt -s histappend
export PROMPT_COMMAND='history -a'
The histappend option appends to history instead of overwriting. PROMPT_COMMAND is run before bash displays a prompt -- this will save each command right after you issue it, instead of when you exit the session.
Now that your history is about to get a lot larger, you'd probably want to override the defaults for HISTSIZE and HISTFILESIZE.
Somewhat OT, but since I mentioned PROMPT_COMMAND: One very useful piece of info to have in your prompt is the exit status of your last command. You can do that with
export PROMPT_COMMAND='ERRVAL=$?; history -a'
Then just include ${ERRVAL} in your PS1.
- litewulf 17y agoI believe that this will cause your history ordering to go a bit wonky. Hitting up to get the last item will almost always get the command you last typed into the current window (as it should be, imo), but if you hit enter a few times after you run a given command it will be something else (sucked in from the surroundings I guess) Every so often this really bites me, but the usefulness far outstrips the quirks.
- timf 17y agoConsidering the number of shells I have open, that would be very weird. A coherent "local" history is very useful not just for the up key but also for referring to history items by number (like "!123"). My solution for my laptop has helped me on countless occasions. I've been doing this for about three years. 1. Compile bash with the syslogger patch. Each command will generate one line with timestamp, username, terminal PID, and command. 2. Configure syslog to pipe all bash commands to a special file. 3. Run a cronjob every night that takes all the bash commands from the day and saves them off to a day specific file. 4. Need to find that command you ran 6 months ago to get package XYZ to work in way ABC? Use "grep -R" to find every single invocation you ever made to that program. If you actually needs a particular shell's local history, pipe grep through a grep for the PID (I find I almost never need that). I work with so many different technologies for such brief times that this is just absolutely invaluable. It also helps you narrow down dates of events sometimes. For installation, use google etc. but here: http://blog.rootshell.be/2009/02/28/bash-history-to-syslog/ http://blog.rootshell.be/2009/02/28/bash-history-to-syslog/ Gentoo users can reinstall with the 'bashlogger' USE flag.
- signa11 17y agoi think it would be supercool if the history function actually stored only the different commands that are being executed, rather than a timestamped version of the command...
- timf 17y agoNot sure about this in the context of that histappend stuff, but bash can do that normally: use HISTIGNORE and "&". Here is my HISTIGNORE: export HISTIGNORE="&:ls:ls -la" That will ignore duplicates, "ls", and "ls -la" That's for local shell history, I also record every command to a file based archive (see my other comment in this thread: http://news.ycombinator.com/item?id=992772 http://news.ycombinator.com/item?id=992772 ).