Repeating Substituted Commands
You will often perform a series of operations on the same set of files; therefore, when you use a command to generate a list of filenames, you may find it desirable to use the list more than once. Suppose you want to revise the "gaboon" files, and then individually format and print each file. You could do the following:
%vi `grep -li gaboon *`%foreach f (`grep -li gaboon *`)?groff -ms $f | lpr?end
However, the method displayed above involves inefficient typing. A backquoted command counts as a single argument in a command line; therefore, you can easily repeat the command via a history operator:
%vi `grep -li gaboon *`%foreach f (`!$`)Repeat final argument of previous command foreach f (`grep -li gaboon *`) ?groff -ms $f | lpr?end
Using your history to repeat a command substitution saves typing, but that method actually runs the substituted command again. If you think that the command may take a long time to complete, running it more than once is inefficient and a waste of time. A different kind of repetition would be preferable.
One way of running a command once, but using its output multiple times, requires that you save the output in a variable, and then refer to the variable when you need to use the list of filenames:
%set args = `grep -li gaboon *`%vi $args%foreach f ($args)?groff -ms $f | lpr?end
Another way to avoid running a time-consuming command multiple times is to save the command's output in a file, and then substitute ...
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