History substitutions, similar to C-shell history substitutions, are also available. History substitutions are prefixed by the character !, or at the beginning of a line, the character ^. Briefly, the string !! is replaced by the previous command, the string !prefix is replaced by the last command with that prefix, the string !?pattern is replaced by the last command containing that pattern, the string !number is replaced by the event with that number, and ^oldpattern^newpattern is replaced by the previous command with newpattern substituted for oldpattern.
Additionally, a !string sequence may be followed by a modifier prefixed with a :. This modifier may select one or more words from the event -- :1 selects the first word, :2-5 selects the second through the fifth word, :$ selects the last word, and :$-0 selects all of the words but reverses their order.
Two other : modifiers are supported: :p will cause the command to be printed but not executed, and :s^old^new will replace the pattern old with the pattern new. The sequence ^old^new is synonymous with !!:s^old^new.
All the commands typed by the user are saved on the history list. This may be examined with the history command, and its maximum length changed by changing the value of the history variable.