Wednesday, May 28, 2008

Bash History

During last 5 years of using Linux, bash has been the application I used most. Yet, I know little about it. As I am writing more and more commands on the console, I am feeling a need for efficiency. This forced me to dig more into bash and its power! But as we all know, people have already done the hard work and kept things ready for us to make use of them.

In this article I would take about Bash history commands and how to efficiently use them. I won't do much apart from providing some good links in this regard:

  1. Deadman.org
  2. http://www.catonmat.net/blog/the-definitive-guide-to-bash-command-line-history/

Summary of some commands:

  • Bash History
    • !! - repeat last command
    • !foo - repeat last command starting with 'foo'
    • CTRL + R - command auto completion
    • history - prints a numbered list of previously typed commands
    • !N - executes Nth command in the history list
    • !foo:p - prints a command without executing it
    • !$ - last argument (word) of the previous command
    • !* - all arguments of previous commands
    • ^error^correction - re-executes previous command by replacing 'error' with 'correction'
  • Home / END
    • CTRL + a - beginning of a line
    • CTRL + e - End of a line
    • CTRL + w - erase a word
    • CTRL + u - erase from here to the beginning of the line

  • Modifying History behaviour

    $ export HISTFILESIZE=500
    $ export HISTIGNORE="&:[ ]*:exit"
    This ignores repeated commands, commands that start with a space and 'exit' and they are not added to bash history

    $ shopt -s histappend
    $ shopt -s histverify
    allows users to verify a substituted history expansion.

    $ shopt -s histreedit
    allows users to re-edit a failed history substitution

    Put these lines into .bashrc file so that you won't have to type them again and again