I rely on my ~/.bash_history extensively to avoid retyping things, both short-term and long term (when I want to look up something I typed yesterday or last week).
First, an obvious necessity is
shopt -s histappend
in ~/.bashrc, so that bash just keeps on adding to the end of the ~/.bash_history file rather than obliterate the file at regular intervals.
By default, bash reads from your ~/.bash_history once when you log in to a terminal, and it updates ~/.bash_history once when you log out. I dislike both of these default behaviors.
Writing out to ~/.bash_history only when you log out is no good, because what if your terminal dies unexpectedly? You'll lose the history of any commands you typed there. This can happen if you kill a bash process, or if the machine powers off unexpectedly, or who knows what. So, to manually update your ~/.bash_history immediatety, at any time you can use this:
history -a
which tells bash to append to ~/.bash_history any commands in the current terminal that aren't already in there.
The second behavior of bash I dislike is that it reads from the history file only at login, mostly because I always have many terminals open at once. I run a command from one terminal, then run a different command from another, switch back and forth etc. These terminals may be appending all kinds of nice new lines to the end of my ~/.bash_history, but because bash never reads the history file except at login, they're never accessible except in the terminal where the command was originally run. Luckily you can force bash to re-read your history file via:
history -n
which tells bash to read any new lines that have appeared in ~/.bash_history since the last time it looked.
Now, say I type a really long command, e.g. a huge long Perl one-liner, in one terminal, and I want to then access and run that command in another terminal via bash history. With bash's default behavior, I can't do it; each terminal has its own history. What I could do is type history -a in the first terminal, and history -n in the second terminal; then it'd work wonderfully.
I find the thought of having to do that all the time tedious, though. Better would be for bash to do it for me every time I run a command. You can accomplish this by putting something like this in ~/.bashrc:
export PROMPT_COMMAND="history -n; history -a"
PROMPT_COMMAND lets you specify a command that bash will run every time it shows you a fresh command prompt, i.e. every time you run a command and the command finishes. So the above tells bash to read any new lines that have appeared in ~/.bash_history since the last time it read it, and then append the last-run command from this terminal to ~/.bash_history, every time you run a command.
So now, if you type a command in one terminal, and want to access it via the history of another terminal, run a command in the other terminal (or just hit Enter) to trigger PROMPT_COMMAND, and then your history will be nicely up-to-date and synchronized with any other terminals you have open. Almost certainly, you'll never notice the tiny bit of overhead caused by bash constantly reading and writing to ~/.bash_history.
See man bash for more info on the history builtin.
16 Comments
Thank you!
shopt -s histappend was exactly what I searched for.
I had found this moths ago at a German blog but could not find it again... Today the "last logout wins" bites me again :(
Now, this should be history :)
Pun intended?
Your use of PROMPT_COMMAND works nicely, but it has one downside: Ctrl-O becomes much less useful. Normally, if you enter a series of commands, you can go back to the first one, and hit Ctrl-O repeatedly to run the current command and retrieve the next. If each terminal saves each command right after running it, then you can't necessarily reconstruct the series of commands run on one terminal.
Yeah, another bad thing is when for example you open a terminal just to start some server. Then you open another terminal and play around for a while. Then you want to restart the server process, so you go back to that terminal and Ctrl+C it and hit UP to go back in your history to re-run the server command. But oops, now a lot of commands are in between you and the one you want.
Maybe it'd be nice to script a way to easily choose which behavior you want on a terminal-by-terminal basis.
[...] this post by Brian today, I will be doing this on all my [...]
Nice post,
That's one of those things I've just dealt with, but never bothered to look into.
shopt -s histappend works perfectly and now I don't have to wonder which session I'm getting my history from :)
Thanks!
, Mike
I find it a lot less confusing this way: alias u='history -n' export PROMPT_COMMAND="history -a;"
Great, it bother me for so long, never took the time to search for a work around and I just get to this page looking for something else and now I've resolve an old problem!
Many Thanks!
I am still new to Linux and I had the same problem as I use many terminals simultaneously. The combination of shopt -s histappend and export PROMPT_COMMAND="history -n; history -a" is very handy. Thank you, I had been looking for something like that for a while. I mean in linux shell, you always have to look at your history, it saves a lot of time.
I had the same problem. Well, half the problem. I just added history -a instead of both -n and -a. With both, you have the problem Brian explained, and after years of being accustomed to one history file per active terminal, it would be too annoying to change that around. By just adding the -a to the bashrc, you can do -n only when you need to.
Thanks for the information. I am just looking for a way to manage my multiterminal environment. shopt -s histappend should be handy tool.
Great, it bother me for so long, never took the time to search for a work around and I just get to this page looking for something else and now I've resolve an old problem! always keep th good work going on your side.. congrats
Oh my gosh! This is brilliant! I wish I knew about this years ago!
Thanks!
Excellent, it bothered me for a long time, but I never took the time to search for a work around and I just get to this page looking for something else and now I have solved my old problem.
Thanks s ton
Thanks!!!
Speak Your Mind
Preview
Commenting Help
Email / Avatar
No HTML allowed!
All HTML is auto-escaped. Use Markdown. Examples:
code in backticks