Departments & Programs

More

Command retry



Last revision August 9, 2004

Table of Contents:

  1. Shell interpretation
  2. Editing command lines
  3. Stuck in a Unix login session?
  4. Program execution
  5. Simple commands
  6. Unix command syntax
  7. Controlling processes
  8. Your login environment

The C-shell also has a history mechanism that remembers your previous commands. These commands can be re-executed, either as is or after some editing.

You type the built-in command

history

to see the list of your previous commands, numbered sequentially since you logged in. You tell the shell how many commands to remember by setting the history variable. The default pangea .login file uses this command to tell the shell to remember your last 20 commands:

set history=20

Saving many commands (for example, > 100) uses a lot of memory and can slow down your interactive response.

You refer to a previous command with the metacharacter !, followed by an additional specification as shown here.

!n
n is a number that refers to the previous command numbered n

!string
string is any arbitrary sequence of characters. This looks through your history list, from most recent (highest numbered) to oldest (lowest numbered), to match the most recent command that starts with the characters in string.

!!
refers to the last command line that you typed (you don't need to know its number)

The ! syntax, in one of its forms shown above, copies the referenced previous command into the shell's input buffer, but does not automatically execute it until the RETURN key is pressed. It does not show this old command on the screen yet. If in doubt about the exact format of the old command, use the history command first to show all the old commands before you re-select one of them with the ! syntax.

You can edit this copy before executing it, using a vi-style substitute command, like:

!!:s/old/new/

which substitutes new in place of the first occurrence of old in the most recent command. Unlike vi, to make a global substitution, put the g character just before the s, not after new.

Finally, after your editing information, if any, you can type more words that will be appended to the end of the copy of the previous command. For example, if you gave the command

ls -l *.f

You could then re-execute it, expanding the argument list to include *.c files, with

!! *.c

which is converted by the shell to

ls -l *.f *.c

Type a carriage return to execute the (possibly edited or appended) copy of the previous command. The shell will type out the command on the terminal for you to see just before executing it.

Note that this history substitution has very high priority in the C-shell, so that the ! character on a command line is always interpreted to mean substitute from a former command, even when it is surrounded by quotes. The only way to force the shell to treat the ! character in a command line as a simple exclamation point, and not try to interpret it, is to escape it with a preceding backslash, like this:

\!

On pangea and some (but not all) other Unix systems, the C-shell has a second, alternate way to edit and re-execute previous commands. Press the ESC key by itself (no RETURN key) in response to the prompt, and you will be put into a special one-line command editor mode.

Logically, you can think of this command editor mode as a simplified vi editor session that uses a screen with only one line (the last line). You start with the screen line positioned on a new blank line, into which you can type a new command.

All the commands that are being saved in your command history (by default on pangea, the last 20 commands) are accessible in this command editor mode. You scroll up and down through the list of commands with the j and k keys, just as you would use them to move the cursor up and down the lines of a file in vi.

Once you find the command you want, you can edit it. Use the vi h and l key commands to move the cursor back and forth over the characters in the line, and the basic vi editing commands to modify the line, such as the x command to delete a character and the i command to switch to insert mode (terminate the new inserted text with the ESC key, just as in vi).

When you are done editing the previous command, re-execute it by simply pressing the RETURN key.

<--Previous Overview Next-->

Comments or Questions?