Solaris Advanced User's Guide

Repeating Previous Commands

The Korn, Bourne Again, C, TC, and Z shells enable your system to keep a history of commands you type and are able to repeat previous commands.


Note –

The Bourne shell (sh) does not support the history command.


Repeating Commands in the Bourne Again, C, TC, or Z Shell

If you use the Bourne Again, C, TC, or Z shell, type !! and press Return to repeat the last command you typed.


example%!!
date
Tue Oct 31 15:18:38 MST 2000
example%

You can also repeat any previously typed command by typing !x, where x is the desired command's corresponding number on the history list. To see the history list, type the history command and press Return. The following is an example of what you might see.


example% history
1  pwd
2  clear
3  ls -l
4  cd $HOME
5  logname
6  date
7  history

Note –

The Z shell does not display the history command in the history list.


Another method for repeating characters from the history list is to follow the ! with a negative number. For example, to repeat the second from the last command on the history list, type the following command.


example% !-2
date
Tue Oct 31 15:20:41 MST 2000
example%

Note –

If you use this command repetition method immediately after the history command in the Z shell, increase the negative number after the ! by one (!-3).


Using the previous example history list, the date command is repeated.

You can also type the ! character, followed by the first few characters of a previous command to repeat that command. For example, if you had previously typed the clear command to clear your screen, you could type !cl to clear your screen again. With this method for repeating commands, however, you must use enough characters for the desired command to be unique in the history list. If you use only one letter after the !, the system repeats the most recent command that begins with that letter.

Repeating Commands in the Korn Shell

If you use the Korn shell, type the following command to repeat the previous command.


$ fc -s -
date
Tue Oct 31 15:18:38 MST 2000
$

You can also repeat any previously typed command by typing fc -s x, where x is the desired command's corresponding number on the history list. To see the history list, type the fc -l command and press Return. The following example is a sample history list.


$ fc -l
344  pwd
345  clear
346  ls -l
347  cd $HOME
348  logname
349  date
350  history
$

You can also repeat commands from the history list by following the fc -s command with a negative number. For example, to repeat the second from the last command on the history list, type the following command.


$ fc -s -2
date
Tue Oct 31 15:20:41 MST 2000
$

Using the previous example history list, the date command repeats.

You can also use the fc -s command with the first few characters of a previous command. For example, if you had previously typed the date command to display the current date, you could type fc -s da to display the date again. However, you must use enough characters for the desired command to be unique in the history list. If you use only one letter after the fc -s command, the system repeats the most recent command that begins with that letter.