Hamilton LaboratoriesHamilton C shell 2012User guideTutorials

History

Oregon Coast

History
Previous | Next

Topics

Recalling and running earlier commands
Retrieving a whole command line
Retrieving individual words
History short-form
See also

Recalling and running earlier commands

The history mechanism lets you recall and rerun earlier commands. To see what it’s remembered, use the history command or its usual alias h, which might show you something like the following:

127 D% history 12 116 (this is the inline data) 117 eof 118 end 119 cat << *** 120 The ^$home directory is $home. 121 Today's date is `date`. 122 *** 123 cat << ^*** 124 The ^$home directory is $home. 125 Today's date is `date`. 126 *** 127 history 12 128 D% _

The history list won’t be quite what you typed: it’ll be broken into separate words wherever one of the special tokens, &, |, ;, <, >, (, ), &&, ||, >> or <<, occurs. Only inline data escapes being broken up into words this way. Each command may be thought of as an array of words, indexed from 0.

To reuse the text or maybe just a few words from a previous command, you type an exclamation point ! followed by a few characters to identify what you want to reuse. You can do this anywhere and whatever you select is just stuffed back on the command line to be interpreted as whatever the context suggests. For convenience, the exclamation point is not treated as a history reference if it’s followed by white space (a space, tab or newline) or by =, ~ or (.

Retrieving a whole command line

There several ways of picking up a whole command line. You already know how to do it interactively with arrow keys and command completion. You can also use a shorthand notation that can be more convenient if you want to do something a bit more complex. The simplest shorthand is !!, which picks up the text of the immediately preceding command:

128 D% echo !! echo history 12 history 12 129 D% !! echo history 12 history 12 130 D% _

The shell first echoes your command showing the effects of the substitutions and then runs it. The other quick ways of referring to a whole command line from history are by the command number,

130 D% !104 ls -l ``whereis ls`` ---A-- Jul 22 08:00 155968 D:\Program Files\Hamilton C shell 2012 x 64\Bin\ls.exe

relative to the immediately preceding command,

131 D% echo one one 132 D% echo two two 133 D% echo three three 134 D% !-1 echo two two 135 D% _

or by mentioning some of the text to look for. A question mark after the exclamation point means you’ll accept the match anywhere on the line; otherwise it has to be at the start.

135 D% !h history 12 124 The ^$home directory is $home. 125 Today's date is `date`. 126 *** 127 history 12 128 echo history 12 129 echo history 12 130 ls -l `whereis ls` 131 echo one 132 echo two 133 echo three 134 echo two 135 history 12

A search string ends at the first word boundary. This is so it’s convenient to type additional text following without having it be confused as part of the search string. For example:

136 D% !?one;!?two;!?thr echo one ; echo two ; echo three one two three 137 D% _

Retrieving individual words

To pick off individual words of the immediately preceding command, there’s some convenient shorthand. !* gets all the argument words:

137 D% echo now is the time now is the time 138 D% echo Finally, !* to begin echo Finally, now is the time to begin Finally, now is the time to begin 139 D% _ “!$” gets just the last word: 139 D% echo the last word was !$. echo the last word was begin. the last word was begin. 140 D% _ and “!^” gets just the first argument word: 140 D% echo ===!^=== time is here echo ===the=== time is here ===the=== time is here 141 D% _

Notice that a history substitution can be smashed right up against other literal text.

In the chapter on editing, additional facilities for selecting individual words or doing a search/replace will be introduced.

History short-form

Recognizing how frequently one would like to make a simple change to the immediately preceding command to correct a typo, the history mechanism provides a short form for just that purpose. % typed as the first character on the command line indicates that a search/replace pair follows:

141 D% echo hello world hello world 142 D% %world%friends% echo hello friends hello friends

Typing %% matches the beginning of the line:

143 D% %%echo % echo echo hello friends echo hello friends

It’s also possible to refer to the search string in the replacement string by using an ampersand. (This example also illustrates that the trailing % isn’t required unless you want to explicitly mark the end of the replacement.)

144 D% %friends%family, & and neighbors echo echo hello family, friends and neighbors echo hello family, friends and neighbors 145 D% _

Obviously, that raises the question: how do you put a literal ampersand in the replacement? Simple. Just quote it with ^, the shell escape character.

145 D% %and%^& echo hello family, friends & neighbors hello family, friends & neighbors 146 D% _

See also

History recall
Order of evaluation

Previous | Next

Getting started with Hamilton C shell

Hamilton C shell, as it first wakes up.

Getting started with Hamilton C shell

A first few commands.

You can set the screen colors to your taste.

You can set the screen colors to your taste.