Command History


Most modern shells (including ksh, bash, csh, and tcsh) keep a list of all the commands you enter during a session. This history list can be used to review the commands you have recently entered or to repeat commands you have used.

You can display a list of previously entered commands with the history command. The following is a typical history list display:

 $ history 113 cd Email 114 ls -l 115 find . -name "*old" -print

 116 cd Save 117 vi draft-old 118 diff draft-old sent-old 119 rm draft-old 120 history

By default, the history command lists all the commands saved in your history file (in some versions of ksh, it might list only the 16 most recent commands). You can change the number of commands the shell saves by setting a variable-HISTSIZE in bash or ksh, and history in csh or tcsh. To display only the most recent commands, run history with an argument:

 $ history 3 121 cp * Backups 122 rm *.old 123 history 3

In ksh, this would be history 3.

The lines in the history list are numbered sequentially as they are added to your history list. If you prefer, you can display your history without command numbers. This is useful if you want to save a series of command lines in a file that you will later use as a shell script. In csh and tcsh, the command to do this is history -h, as in

 % history -h 7 > newscript           # csh or tcsh

which saves the eight most recent commands in the file newscript. In bash or ksh, the equivalent command would be

 $ fc -In -7 > newscript             # ksh or bash

The command history list is preserved in a file across sessions, so you can use it to review or repeat commands from previous login sessions. The name of the file is specified by a shell variable-HISTFILE in bash and ksh, histfile in csh or tcsh. In addition to viewing commands from your history list, you can use your history list to redo previous commands. This is made possible by the history substitution feature. The syntax for history substitution is significantly different in the various shells. Table 4–4 shows the similarities and differences.

Table 4–4: History Substitution

ksh

bash

csh or tcsh

Effect

history

history

history

List commands in history

history-n

history n

history n

List n most recent commands

fc-ln

fc-ln

history -h

List history without line numbers

r

fc-s

!!

Repeat previous command

r n

fc -s n

!n

Redo command number n

r -n

fc -s -n

!-n

Redo nth most recent command

r cmd

fc -s cmd

!cmd

Redo most recent instance of cmd

History Substitution in csh and tcsh

History substitution is similar to the variable substitution discussed earlier in this chapter (and to command substitution, which will be discussed later). An exclamation mark at the beginning of a line tells csh or tcsh to substitute information from your history list.

Suppose you recently used the vi editor (discussed in Chapter 5) to edit a file named cs106xProject.c. If you want to do more editing on that file, you can use the history substitution feature to redo the command without having to retype it. For example,

 % !vi vi cs106xProject.c

repeats the last command beginning with vi. Note that the command automatically supplies the name of the file in this case. In general, it repeats all of the arguments to the command.

You can use command numbers from your history list to redo commands. The exclamation mark followed by a number repeats the history list command line with that number. For example, to repeat command number 114, you would type

 % !114 ls -1

A number preceded by a minus sign tells the shell to go back that many commands in the list. If the last command you entered was number 119, the following command would take you back to command 116:

 % !-3 cd Save

A very useful shorthand for repeating the previous command is two exclamation marks, as in the following:

 % !! 

This repeats the immediately preceding command.

In any of the previous examples, you can print the command without executing it by adding :p at the end, as in

 % !!:p

History substitution can also be used to edit commands, and to copy commands or arguments from your history list into your command line. Although these features can be useful, they are difficult to remember and have to some extent been replaced by command-line editing, described in the next section. If you are determined to learn the full set of history substitution commands, see http://www.npa.uiuc.edu/docs/tcsh/History_substitution.html.

History Substitution in ksh and bash

In bash, the command fc -s is used to repeat commands. In the Korn shell, the alias r is used as a more memorable shortcut for fc -s. This alias is automatically defined by the shell. To repeat your most recent command in ksh, type

 $ r

In bash, this would be

 $ fc -s

To use the r command in bash, just add the line

 $ alias r='fc -s'

to your .bashrc.

To repeat a specific command from your history list, type r followed by the number. For example, to repeat command 114, you would type

 $ r 114                              # fc -s 114 ls -1

A number preceded by a minus sign tells the shell to go back that many commands in the list. If the last command you entered was number 119, the following command would take you back to command 116:

 % r -3                               # fc -s -3 cd Save

You can also redo commands by specifying the command name. In this example,

 $ vi cs106xProject.c $ ls cs106xProject.c   ProjectBackup $ r vi                               # fc -s vi vi cs106xProject.c

r vi repeats the last command beginning with vi.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

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