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 < !$ | moreRun anova, using temp as input file %^more^lprChange more to lpr and rerun command %!sed:s/datal/data2Rerun sed, changingdata1todata2%!?moreRerun most recent command that contains more %!?lprRerun 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 ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access