Bash history tips and tricks
Commands you type at a bash prompt are likely stored in what is known as your bash history. Some minor tweaks to how this history is written can be quite handy. Here are the ones I have found most useful, in no particular order:
- Add datestamps
While you don’t need this for using history merely as a list of things run, it can be very handy to know when you ran various things, in case you need to do any investigating and troubleshooting. You just add1export HISTTIMEFORMAT=and then the format you want into your .bashrc. I like:
1export HISTTIMEFORMAT='%Y-%m-%d %H:%M:%S - ' - Append versus overwrite and write more often to handle multiple sessions
By default, bash overwrites history across sessions, instead of appending. So if you have a prompt on a box, then open another and close that, all commands entered in the first get lost. Not cool. Easy fix, add this to .bashrc:1shopt -s histappendIn addition, you might find you need commands you typed in one window in another, before you have logged out of the first. To get around this, you can have history get written whenever a prompt is shown. Add to .bashrc:
1PROMPT_COMMAND='history -a'Now that command is run just before any prompt is shown, so your history file immediately has your last command. One problem: while your .bash_history will be updated, running the history command will not show the new items. For that, you need to re-read the history file. You can handle both of these with:
1PROMPT_COMMAND='history -n;history -a'Each time you are shown a prompt you now write to history and re-read it. Thus you have access to commands run in in other sessions. One caution: Running your “last” command might now not give you what you expect. If you run a command in terminal A, then another in B, and then try “sudo !!” in A, you might want it to run the last command from A, but you will run the last from B, since that is now between current and the last command from A. So be careful until you get used to it.
- Increase the number of commands recorded
By default, only the last 500 bash commands are saved. If you are on boxes every day, or need a long term record, this won’t cut it. Push this up to whatever suits your fancy with:1
2HISTSIZE=100000
HISTFILESIZE=100000The first variable specifies the number of commands to record in the command history, while the second specifies the maximum number of lines the history file should contain. These will likely be the same unless you write a lot of multiple line commands. I just set them the same, and both very large.
- Handle multiple line commands
Speaking of multiple line commands, commands taking up multiple lines (via e.g. “\”) get shown as several entries in history. You can set:1shopt -s cmdhistThis turns on the setting for multiple line commands to be stored in the history as a single command, which is almost definitely what you want.
- Dupes, corrections, and ignoring
I like to use the bash history as a log of what I recently did as well as a handy shortcut to recent commands. Because of the first requirement, I myself don’t make any changes to history. I want just what I typed going in there. But if you so desire, you can clean things a bit before they are recorded:- Ignore dupes and whitespace: The following variable will result in commands starting with a whitespace character or that are duplicates of the last command not being written to history:
1HISTCONTROL=ignoreboth
- Spelling: The following variable ignores common spelling/path mistakes:
1shopt -s cdspell
- Ignore dupes and whitespace: The following variable will result in commands starting with a whitespace character or that are duplicates of the last command not being written to history:
How can you interact with this lovely improved history? Glad you asked. To just see what is present, run “history” or “history | tail” for the latest items. This would be an easy way to access history as a log of events. Other ways to use history:
- Up arrow: This is usually the first way people come across history in bash. You can press the up arrow to view the last command entered, and keep pressing up to scroll through your history in reverse. Moderately handy, until you discover…
- !!: The characters “!!” expand to the previous command. I use this all the time when I forget to sudo something:
1
2
3
4kill 5637
bash: kill: (5637) - Operation not permitted
sudo !!
sudo kill 5637Much easier than typing it out again, and slightly faster than the up arrow, home key, “sudo ” combo. ctrl + p also inserts the previous command, just a matter of preference.
- If you know something about the command you want to retrive, but it was used a while ago, use ctrl + r. After pressing that, start typing something that was in the command. The first match is pulled up. If it isn’t the one you want, do ctrl + r again to view the next match. If that still isn’t getting you anywhere, you might have to resort to the longer but effective: “history | grep thing”
Once you start writing longer and more complicated commands, it is very handy to be able to quickly retrieve and re-run them. And with bash history, it’s easy!
Possibly Related (no promises):
Related posts brought to you by Yet Another Related Posts Plugin.
November 8, 2008 - 12:21 AM










shu
April 15, 2009 | 4:12 AMmy solairs 10 cann’t work with history -n
command
shu
April 15, 2009 | 4:13 AMwhen i use this, a command will be logged hundreds times in history file.
Samuel Huckins
April 15, 2009 | 6:28 AMSorry, can’t make any promises for Solaris. I only get to work on Linux boxes. From the docs ( http://docs.sun.com/app/docs/doc/816-0210/6m6nb7mbl?a=view ) however, it appears -n should work the same.
farbige kontaktlinsen
October 20, 2009 | 12:28 PMWhen I hear this, I am really becoming a fan of twitter.