tail -f findings.out

« PuTTY + Launchy = FTW

Vista: You only get screenshots if you have a Tablet-PC »

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 add

    1
    export HISTTIMEFORMAT=

    and then the format you want into your .bashrc. I like:

    1
    export HISTTIMEFORMAT='%Y-%m-%d %H:%M:%S - '

    Which produces

    versus the default

  • 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:

    1
    shopt -s histappend

    In 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:

    1
    PROMPT_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:

    1
    PROMPT_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
    2
    HISTSIZE=100000
    HISTFILESIZE=100000

    The 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:

    1
    shopt -s cmdhist

    This 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:
      1
      HISTCONTROL=ignoreboth
    • Spelling: The following variable ignores common spelling/path mistakes:
      1
      shopt -s cdspell

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
    4
    kill 5637
    bash: kill: (5637) - Operation not permitted
    sudo !!
    sudo kill 5637

    Much 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!

Share and Enjoy:
  • email
  • LinkedIn
  • Slashdot
  • StumbleUpon
  • Technorati
  • Netvibes

Post to Twitter Post to Delicious Post to Digg Post to Reddit

Possibly Related (no promises):

  1. Useful Bash functions to determine OS and more

Related posts brought to you by Yet Another Related Posts Plugin.

Tags: , , ,
November 8, 2008 - 12:21 AM
5 comments »
  1. Improving command line efficiency via Bash history | tail -f findings.out
Leave a reply

Subscribe without commenting

Twitter links powered by Tweet This v1.6.1, a WordPress plugin for Twitter.