Use Bash more efficiently: History – Part 2

4 minute read

This post is a sequel to the earlier post: Use Bash more efficiently: History – Part 1, I hope you remember the things I described in it. I recommend just trying out all the commands from the first post on your terminal. Practically using is more important than knowing.

Topics for today:

  1. More advanced operators.
  2. Modifying the history.
  3. Customizing the history.
  4. Disabling history to execute Secret commands 😉 .

More Advanced Operators:

  • ? operator :

    The ? bash history operator is similar to the * wild-card character. Yes, and I didn’t mistype * here 😛 . Lets see some examples:

    $ !da?

    The above command prints and executes the most recent command that starts with da, e.g. the date command.

    $ !?thread?

    The above command prints and executes the most recent command that has thread in it, e.g. compiling a thread program that you did some time ago.</li>

  • ^ or the quick substitution operator :

    The ^ bash history operator runs the last command again replacing one string with another. For Example:

    $ gcc -fPIC library.c
    $ ^-fPIC^-shared

    The second command above evaluates to:

    gcc -shared library.c
  • The -p option :

    What if you just want to see what command will be executed if you write a particular history command line but don’t actually want to execute the command? You can do this using the -p option. For Example:

    $ history -p !-2

    The above command just prints and doesn’t execute the second last command.</li> </ul>

    Modifying the history:

    • Deleting an Entry :

      You can delete a particular numbered entry from your history using the -d option. Lets see an example:

      $ history -d 2000

      The above command simply deletes the 2000 numbered entry from the current history, i.e. before adding this command to the history. Just see the illustration below to understand:

      $ history 5
       2016  history
       2017  history -d 2010
       2018  history
       2019  history 10
       2020  history 5
      $ history -d 2019
      $ history 5
       2017  history -d 2010
       2018  history
       2019  history 5
       2020  history -d 2019
       2021  history 5
    • Adding an Entry :

      You can add an entry into the history without executing that command using the -s option. For Example:

      $ history man man

      Adds man man command to history without it getting executed.</li>

    • Playing with the .bash_history file :

      Bash stores the history in the .bash_history file in your home directory or the file specified by $HISTFILE shell variable. Each time we start the bash shell, the history is loaded from this file and each time we exit the shell the history is saved to this file. The maximum size of this file is determined by the shell variable $HISTFILESIZE. You can save or load the current history to or from another file using -w and -r options respectively.

      $ history -w filetowriteto.txt
      $ history -r filetoloadfrom.txt

      Also, see:

      $ history -a
      $ history -n
      $ history -c

      The -a option appends the current history to the history file. The -n option reloads the history from the history file. The -c option deletes the entire history.</li> </ul>

      Customizing the history:

      You can customize how history works for you using the shopt bash built in command. It allows you to change additional shell optional behavior. Just run shopt on your shell and you will see the current state(on/off) of all the optional shell behaviors.

      Usage: shopt [-pqsu] [-o] [optname …]

      The most important options are:

      1. -s : Enable (set) each optname.
      2. -u : Disable (unset) each optname.
      3. -o : Work on special set built in options.

      So, to see all the shell options related to history execute the following command:

      $ shopt|grep hist
      cmdhist        	on
      histappend     	on
      histreedit     	off
      histverify     	off
      lithist        	off

      Lets see these one by one:

      1. histappend : If this shell option is on, each time we exit our shell the history is appended to the .bash_history file.
      2. histverify : As you have seen so far, whenever we execute a history related command, it immediately gets executed.  What if we want to edit it first and then execute it? You can do this if the histverify option is on. But it is off by default, so to enable it do:
        $ shopt -s histverify

        Now, try:

        $ !-3
        $ gcc abc.c (cursor blinking here - not executed yet. Edit and then press enter to execute.)
      3. cmdhist : Saves multiple line commands in the history.

      Disabling history:

      So, finally we are going to learn how to disable history, so that all the commands that you execute from now on are not recorded in history. Very handy for doing something nasty isn’t it 😉 😛 . This is done by turning off one of the shell options which you haven’t seen yet. Well, see it now:

      $ shopt -o | grep hist
      history      on

      So just turn this off like this:

      $ shopt -u -o history

      And now execute any command 😉 , it would not show up in history. And after you are done with your nasty task, don’t forget to re-enable it so nobody gets to know 😛 .

      $ shopt -s -o history

      So that’s all for the Bash history command. This is a bit longer post but I hope you like it. Please comment if you have any suggestions, doubts, etc.

      Also don’t forget to subscribe to automatic updates direct to your email, if you haven’t done that yet, below:




Leave a Comment