History Substitution

History substitution allows you to use words from previous command lines in the command line you are currently typing. This makes it easier to correct simple errors in complicated command lines. To do this, the shell has a built-in mechanism that keeps track of some number of the commands you have typed. For example, the command "set history=10" in your '.cshrc' file will tell the shell to remember the last 10 commands you have typed: % history 1 ls 2 date 3 history % This shows that I have given 3 commands: "ls", "date", and "history".

A history substitution command begins with an exclamation point or "bang sign" (!). The command "!!" will repeat your previous command. If you give the "date" command and then "!!", the "date" program will run again:

% date Fri Sep 4 14:21:01 PDT 1992 % !! date Fri Sep 4 14:21:01 PDT 1992 % Here are some ways you can use history substitutions:
!!
re-run the previous command
!n
re-run command on line number n (from the history listing)
!string
re-run last command beginning with string
^word1^word2
In previous command, substitute word2 for word1.

Click here to go to the next section.

Or click here to go to the top of the chapter.