Shell History


What is the Shell's History?

Almost every shell out there (tcsh and bash included) store your previous commands that you have issued.

I think that the way that tcsh is configured by default on the GL system, it remembers the last 100 commands that you have entered into the shell. Last I checked bash, is configured to default to remembering the last 1000 commands that you have entered into the system.


Why Should I Care About the Shell's History?

Well most shells (tcsh and bash included) allow you to press the up arrow to cycle through previous commands. There previous commands are what makes up the history.

You can save some time by scrolling up through the already executed commands and execute them exactly as the are or make small alterations as needed.

But this is just the top of the iceberg as to what all we can do with this history that is being stored. Let us examine some of the other possibilities...


How Do I See What is in the Shell's History?

Well to see what commands we have entered (or at least as many as the shell is configured to remember) we use the history command...

linux2 [6]# history
     1  20:32   ls
     2  20:32   cd courses/
     3  20:32   ls
     4  20:32   emacs README
     5  20:32   who
     6  20:32   history
linux2 [7]# 

There are a couple of things to note. The leftmost number is the history number that is associated with an executed command. Next is the time that the command was executed. Lastly is the command that was executed.


Can I Clear the Shell's History?

Yes you can. Most shells support the "-c" option. This allows you to clear the history...

linux2 [6]# history
     1  21:25   ls
     2  21:25   cd courses/
     3  21:25   ls
     4  21:25   emacs README
     5  21:25   who
     6  21:25   history
linux2 [7]# history -c
linux2 [8]# history
     8  21:25   history
linux2 [9]#

! - a.k.a. Bang

We access access commands in the history and re-execute them using a single exclamation mark (!), many UNIX users also refer to it as Bang. There are 2 ways to use the single exclamation mark (!).

We can type the command "!" followed by a history number to re-execute that command...

linux2 [4]# history
     1  21:47   gcc hello.c
     2  21:47   a.out
     3  21:47   emacs hello.c
     4  21:47   history
linux2 [5]# !1
gcc hello.c
linux2 [6]#

You can also type the command "!" followed by the first character(s) of that command. When in doubt, the shell will always execute the most recently executed match...

linux2 [7]# history
     1  21:47   gcc hello.c
     2  21:47   a.out
     3  21:47   emacs hello.c
     4  21:47   history
     5  21:47   gcc hello.c
     6  21:49   gcc round.c
     7  21:49   history
linux2 [8]# !g
gcc round.c
linux2 [9]#

!! - a.k.a. Bang Bang

We can also always execute the most recently executed command (the last one) by ussueing the command "!!"...

linux2 [10]# gcc -Wall -ansi hello.c
linux2 [11]# !!
gcc -Wall -ansi hello.c
linux2 [12]#

Daniel J. Hood
Last modified: Thu Jan 16 21:55:59 EST 2003