Grepping the ‘history’ command

2 minute read

I was updating my previous blog post when I remembered that I wanted to include a command that I ran but couldn’t remember. So I looked up how to grep the history command and thought I’d write make a small post about it to help solidify the ability to ‘pipe’ commands.

‘Grepping’ history

To grep the history command all you have to do is:

  • history | grep something_to_grep

Examples

Here are two examples of using grep with history:

  • history | grep test Example history usage against the 'test' string
  • history | grep git Example history usage against the 'git' string

Managing your history list

Thinking about this, there seems to be a lot that gets ‘remembered’ in your history. It would be cool if you could filter what gets saved to history in order to make finding past commands easier, particularly when using git since the same commands are used so frequently.

I am using zsh so I am going to list some of the environment variables supported by this shell for manipulating the history. This is the manual link that I referenced.

  • By default, the history is saved to the ~/.zsh_history, this is stored in the HISTFILE and can be changed if desired
  • By default, the $HISTSIZE will be 2000 entries long, but if you use Oh My Zsh like I do, then it will be increased to 50,000. I tried finding where this is done since it appears to overwrite the default HISTSIZE that zsh defines, and this line of code in the public Oh My Zsh repo is the best I could come up with. It also looks like there may be a bug with regard to the relationship between the definitions of this environment variable
  • zsh uses HISTORY_IGNORE instead of the HISTIGNORE variable that bash uses

Additional references

I was perusing different documentation and posts and saw all sorts of ways that the history can be manipulated, so rather then try to give recommendations (for now), I’ll link to some references I read when learning about how the history file can be manipulated:

Conclusion

There is clearly a lot to learn about shell history and the history command. I hope to learn more about customizing zsh history, and when I do, if there is something that I feel should be added to this blog, then I’ll do it. Otherwise, hopefully these references help others on their learning paths. :)