Section 8.8. History


[Page 310 (continued)]

8.8. History

The C shell keeps a record of the commands that you enter from the keyboard so that they may be edited and re-executed at a later stage. This facility is sometimes known as a history mechanism. The ! metacharacter gives you access to history.

8.8.1. Numbered Commands

When you're using history, it's very handy to arrange for your prompt to contain the "number" of the command that you're about to enter. To do this, insert the \! character sequence into your prompt:

% set prompt = '\! % '      ...include event num in prompt. 1 % echo Genesis      ...this command is event #1. Genesis 2 % _     ...the next command will be event #2. 


8.8.2. Storage of Commands

A C shell records the last $history commands during a particular session. If $history is not set, a default value of 1 is used. If you want these commands to be accessible from the next session, set the $savehist variable. If you do this, the last $savehist commands are maintained in the file specified by the HISTFILE variable (defaults to $HOME/.history). A history file is shared by all of the interactive C shells created by the same user unless HISTFILE is purposely set to a unique value in each different shell. In the following example, I instructed my shell to remember the last 40 commands in my history list and to store the last 32 commands between sessions:

2 % set history = 40      ...remember the last 40 commands. 40 3 % set savehist = 32     ...save 32 across sessions. 32 4 % _ 


8.8.3. Reading History

To obtain a listing of a shell's history, use the built-in command history (Figure 8-17).


[Page 311]

Figure 8-17. Description of the history shell command.

Shell Command: history [ -rh ] [ number ]

history allows you to access a shell's history list. If no parameters are supplied, this command lists the last $history commands. The -r option causes the history list to be listed in reverse order, and the -h option inhibits the display of event numbers. "history" is usually aliased to "h" for speed.


Here's an example:

4 % alias h history       ...make a useful alias. 5 % h       ...list current history.     1  13:25   set prompt = '\! % '     2  13:26   set history = 40     3  13:26   set savehist = 32     4  13:26   alias h history     5  13:27  h 6 % h -r 3     ...list last 3 commands in reverse order.     6  13:27  h -r 3     5  13:27  h     4  13:26   alias h history 7 % _ 


8.8.4. Command Re-execution

To re-execute a previous command, use the ! metacharacter in one of the forms shown in Figure 8-18.

Figure 8-18. Command re-execution in the C shell.

Form

Action

!!

Replaced with the text of the last command.

!number

Replaced with the text of the command with the specified event number.

!prefix

Replaced with the text of the last command that started with prefix.

!?substring?

Replaced with the text of the last command that contained substring.


These sequences may be used anywhere in a command line, although they're usually used in isolation. The recalled command is echoed to the terminal before it is executed. The value of prefix or substring may not contain a space. The special meaning of ! is not inhibited by any kind of quote, but may be inhibited by preceding it with a space, tab, =, (, or \. Here are some examples:


[Page 312]

41 % echo event 41       ...a simple echo. event 41 42 % echo event 42       ...another simple echo. event 42 43 % !!            ...re-execute last command. echo event 42      ...echo command before re-execution. event 42 44 % !41           ...re-execute command #41. echo event 41      ...echo command before re-execution. event 41 45 % !ec           ...re-execute command starting with "ec". echo event 41      ...echo command before re-execution. event 41 46 % _ 


8.8.5. Accessing Pieces of History

You may access a portion of a previous command by using history modifiers. These are a collection of options that may immediately follow an event specifier. Each modifier returns a single token or range of tokens from the specified event. Figure 8-19 is a list of the modifiers.

Figure 8-19. C shell history modifiers.

Modifier

Token(s) returned

:0

first

:number

(number+1)th

:start-end

(start+1) th through to (end+1)th

:^

first

:$

last

:*

second through to last


The colon before the ^, $, and * options is optional. To use one of these modifiers on the last command, you may precede the modifier by "!!" or just "!". Here are some examples:

48 % echo I like horseback riding   ...original line. I like horseback riding 49 % !!:0 !!:1 !!:2 !!:4         ...access specified arguments. 
[Page 313]
echo I like riding I like riding 50 % echo !48:1-$ ...access range of arguments. echo I like horseback riding I like horseback riding 51 % _


8.8.6. Accessing Portions of Filenames

If a history modifier refers to a filename, it may be further modified in order to access a particular portion of the name. The existing modifiers may be followed immediately by the following filename modifiers (Figure 8-20).

Figure 8-20. C shell filename modifiers.

Modifier

Part of file

Portion of the specified fileName that is returned

:h

head

The filename minus the trailing pathname.

:r

root

The filename minus the trailing .* suffix.

:e

extension

The trailing .* suffix.

:t

tail

The filename minus the leading directory path.


In the following example I accessed various portions of the original filename by using this filename access facility:

53 % ls /usr/include/stdio.h        ...the original. /usr/include/stdio.h 54 % echo !53:1:h        ...access head. echo /usr/include /usr/include 55 % echo !53:1:r        ...access root. echo /usr/include/stdio /usr/include/stdio 56 % echo !53:1:e        ...access extension. echo h h 57 % echo !53:1:t        ...access tail. echo stdio.h stdio.h % _ 


8.8.7. History Substitution

The substitution modifier is replaced by the specified portion of a previous event after a textual substitution is performed. Figure 8-21 gives the syntax.


[Page 314]

Figure 8-21. Description of C shell history substitution.

!event:s/pat1/pat2/

This sequence is replaced by the specified event after replacing the first occurrence of pat1 by pat2.


Here's an example:

58 % ls /usr/include/stdio.h          ...the original. /usr/include/stdio.h 58 % echo !58:1:s/stdio/signal/       ...perform substitution. echo /usr/include/signal.h /usr/include/signal.h 59 % _ 


It's quite common to want to re-execute the previous command with a slight modification. For example, say you misspelled the name of a file. Instead of typing "fil.txt", you meant to type "file.txt". There's a convenient shorthand way to correct such a mistake. If you type the command:

^pat1^pat2


then the previous command is re-executed after the first occurrence of pat1 is replaced by pat2. This shortcut procedure applies only to the previous command. Here's an example:

% ls -lG fil.txt           ...whoops! ls: File or directory "fil.txt" is not found. % ^fil^file                ...quick correction. ls -lG file.txt            ...OK. -rw-r-xr-x   1 ables       410 Jun  6 23:58 file.txt 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net