Bash history cheatsheet

When I was researching for the previous blog post, I found a lot of information about setting up shortcuts for interacting with the bash history functionality. It seems that a lot of people want to focus on the past.

I wish that I only knew this much about bash history (I know more, but I wish that I did not):

  • Use "history -w" to write the current session's history to the history file when there is something that you want to save.
  • Use "history -r" to read the history file into your session when you have written to the history file from another terminal and you want to have access to an updated command list.
  • Use "history | grep foo" to search history.
  • Use "sudo !!" to run the last command as root, when necessary.
  • Use "history" to show a list of recent commands. Use !NUM to re-run a command where NUM is the number of a command that was just listed when you typed "history". Ex: "!42".
  • You can edit the history file in a text editor. The file is ~/.bash_history .

I wrote this stuff on a 3x5 card and it changed my life.