Reusing and Editing Commands

We've just seen some techniques for typing filenames easily. The following sections show how to use some of the other features that the shell provides for reducing typing: reusing and editing commands, using aliases to create command shortcuts, and command substitution.

The Shell's History List

The shell has a mechanism for maintaining a history list. That is, it remembers the commands you execute and allows you to rerun them with a few keystrokes. Suppose you're analyzing two data sets, reviewing the results of each analysis before printing the data:

% sed -e 's/^\(...\) *\([0-9][0-9]*\)/\2 \1/' data1 > temp
% anova Height Weight < temp | more
% anova Height Weight < temp | lpr
% sed -e 's/^\(...\) *\([0-9][0-9]*\)/\2 \1/' data2 > temp
% anova Height Weight < temp | more
% anova Height Weight < temp | lpr

There is a lot of redundancy in these commands. You can get the same results with less typing by reusing commands that you've already run:

% sed -e 's/^\(...\) *\([0-9][0-9]*\)/\2 \1/' datal > temp
% anova Height Weight < !$ | more         Run anova, using temp as input file
% ^more^lpr                               Change more to lpr and rerun command
% !sed:s/datal/data2                      Rerun sed, changing data1 to data2
% !?more                                  Rerun most recent command that contains more
% !?lpr                                   Rerun most recent command that contains lpr

Before you try reusing commands, make sure that your shell is remembering them. Try the history command to display your history list:

% history 209 rm junk 210 pushd 211 ls 212 vi ch01 ...

Get Using csh & tcsh now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.