The command history

The command

history

displays a numbered history list of recent commands in the order in which you have used them.

Reusing Commands

For...

Type...

previous command

!!

command number n

!n

most recent command starting with string

!string

most recent command containing characters that match string

!?string

1. To re-run the previous command:

history
      1  pwd
      2  cp memo.txt /tmp
      3  cd
      4  history
!!
      1  pwd
      2  cp memo.txt /tmp
      3  cd
      4  history
      5  history

The history command is used to list the command history. Then the previous command is reexecuted, listing the command history again!

2. To re-run a specific command:

   !3
   cd

This re-runs command number three (3) in the command history.

3. To re-run the most recent command that starts with a particular character string:

   !pw
   pwd
   /home/grace

This re-runs the command pwd.

4. To re-run the most recent command that contains a particular character string:

   !?st
   history

This re-runs the history command.

Previewing a command from the history list

You can preview a command from the history list without having to re-run it.

To...

Do this...

Preview the previous command.

!!:p

Preview command number n

!n:p

Preview most recent command starting with characters in string

!string:p

Preview most recent command containing characters that match string

!?string:p

Editing a previous command line

You can edit any previous command from the history list to create a new command. Suppose that command number 23 in the history list is:

   23 lpr part[1-4].ps

To change [1-4] to [5-8] use the command:

   !23:s/[1-4]/[5-8]/

This will now run the command:

   lpr part[5-8].ps

Changing the size of the command history

The standard ISS Unix environment sets the size of the command history to 25.

To save a larger number of commands set the history shell variable to a larger number. For example:

   set history=50

This will keep a history of the fifty (50) most recent commands that you have used.

Saving command history between logins

To save your command history between one login session and another set the variable savehist in your .login file. For example:

   set savehist=10

saves the previous 10 commands you have used to the file .history in your home directory whenever you logout.

When you next login the contents of the file .history are placed in the history list. You can then re-use commands from your previous login session.

Too high a value for the savehist variable will slow down the login procedure. Do not set the value of the savehist variable higher than that of the history variable.