A little bit of history

By  

You can selectively remove commands from your history file using history -d:

$ history 6
   42  echo 1
   43  echo 2
   44  echo 3
   45  echo 4
   46  echo 5
   47  history 6
$ history -d 45
$ history 6
   43  echo 2
   44  echo 3
   45  echo 5
   46  history 6
   47  history -d 45
   48  history 6

The -d option is particularly handy if you completely botched a command and want to be sure not to repeat the mistake when you re-use commands.

You can clear your history buffer with the -c argument, though the old entries in your history file will remain intact until you log off.

$ history -c

You can also modify commands and run them again by using the ^old^new^ syntax.

$ cal 10 2012
    October 2012
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

$ ^0^1^
cal 11 2012
   November 2012
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

Typing ^r (control-r) allows you to search your history for particular strings regardless of where the strings appear within your prior commands -- thus more flexible than the !l syntax. You might want to locate and re-run the last command you ran against a particular file or using a particular filter. Type ^r and you will see this prompt asking you for your search term:

(reverse-i-search)`':

How much history you preserve depends on your HISTSIZE setting. Generally set to 1000 commands, this allows you to reach back pretty far into your command history. If you don't want these lines to preserve all of your ls, pwd, bg, fg, history and exit commands, you can exclude these commands by setting your HISTIGNORE. Just remember to add a line like this to your .bash_profile.

export HISTIGNORE="&:ls:pwd:[bf]g:history:exit"

Using shopt

If you are staring at that heading and asking yourself "shopt? what's that?", you're not alone. This command isn't in the most likely Unix commands to be used. In fact, it's not really a command, but a shell built-in. So you won't find it using the which command, but the man page should provide a lot of good information. The word "shopt" doesn't suggest its use if you, like me, see "shop tee". If you can think of it instead as "sh opt" (shell options), it will be a little easier to remember. The shopt built-in allows you to toggle settings that control shell behavior, some of which affect how the history command behaves.

In the shopt output below, you can see whether various features of the shell are turned on or off.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question
randomness