Bash Goodies: Turbocharging your History

Everybody knows you can get a list of recently run commands in your bash shell using the history command. And it’s commonplace to use grep to search through that output to find some commands of interest. But doing so is cumbersome:  you may have to issue a bunch of different


commands before you can find the command you were looking for, and — once found — you have to copy and paste that command from the output to the command line (or use the dangerous ‘!’ operator). And if it’s been a while, there’s a good chance the command has fallen off the end of your history file. Or, if that command was issued in another open shell session, it will be invisible to you. These are all problems we address in this Bash Goodies installment!

1. Use reverse-i-search to power through your history.



at the bash command prompt to start a reverse-i-search through your history file. As you type each character, bash will interactively display the last command in your history which contained what you’ve typed so far. Hit


again to take the cursor to the yet-previous instance of that substring (whether in the current command or a previous one), repeating as necessary. You can use backspace to edit the search string at any point, or hit


if you’d like to exit out of the search. If you see the command you want to reissue, you can just hit


. Or, you can hit the left or right arrow key to begin editing the command so you can issue a modified version. (And if you want to get fancy, you can use




, killing and yanking, respectively, to combine bits and pieces of different commands.) Once you’ve gotten the hang of reverse-i-search you’ll probably find it much more powerful and convenient than


ping or (gasp) scrolling through your history with up and down arrows.

2. Make your history file big.

It doesn’t usually pay to be stingy with your history file. At some point you’ll regret not being able to summon up that magic one-liner for counting the number of roman numerals in all your text files created on odd-numbered days.  By default, bash keeps around only your most recent 500 commands. We’ve been storing the last 10000 with no ill effects, using these additions to our



export HISTSIZE=10000
export HISTFILESIZE=10000


sets the number of commands to save during any one session, and


sets the number of commands to save between sessions. It’s best that they be equal.

3. Instantaneously share history between bash sessions.

If you’re doing some work across multiple open terminals, it’s easy to lose track of which commands were entered where. This makes your history files terminal-dependent, and often less useful. But if you add these lines to your


file, your history will be synchronized across sessions every time you enter a command!

shopt -s histappend
export PROMPT_COMMAND="history -a; history -n; $PROMPT_COMMAND"

The first line configures bash to append to rather than overwrite your history file. The second says that every time bash generates a fresh command prompt, it should append the last run command to the history file, and load any new commands written to that file (from other shells) into the current history list.  Since these commands run when the command prompt is set, you’ll need to enter a command (or just hit


) in the current shell to see new additions. (If you’d rather refresh the history on demand for old-school up-arrow behavior, remove the

history -n

part of that line and run it manually when desired.)

Another nice aspect of updating your history file after each command is that if you have to kill a shell without logging out (or you log in remotely before exiting that shell), you’ll still have access to your history.

4. Bonus: Add timestamps to your history.

If you add this to your


file, your history file will contain the date and time that each command was issued:


…Use Wisely

Of course, relying on your history file should never take the place of good documentation and scripting of your workflow. And one must be careful with a turbocharged history — it’s easy to reverse-i-search your way to a dangerous command and accidentally hit enter. But hopefully the above tips, which we’ve been using to good effect here at Numerate, will save you some time and hassle next time you realize that the “one-off” command you issued last week is going to come in handy today.  As they say, “Those who cannot remember the past are condemned to repeat it (probably with a mistake thrown in)!”

Tags: , ,

Comments are closed.