10.3 C Shell Aliases with Command-Line ArgumentsIt's convenient for your aliases to use command-line arguments. For example, let's think about an alias named phone : alias phone 'cat ~/phonelist | grep -i' After you define that alias, you could type phone smith . The shell would find the phone alias and execute it with the argument (smith ) at the end (10.2 ) this way: cat ~/phonelist | grep -i smith Using cat and a pipe that way is inefficient (13.2 ) . It might be more sensible to have an alias that worked like this: grep -i
How do we do this?
The C shell's
history (11.7
)
facility lets us use the
notation alias phone grep -i \!$ ~/phonelist When we use the phone
command, its final argument will be substituted
into the alias. That is, when we type
In this example, we needed another kind of quoting. We had to put a
backslash before the exclamation point to prevent the shell from
replacing But why couldn't we just use single quotes or double quotes (8.14 ) ? This isn't the right place for a full explanation, but neither single quotes nor double quotes protect the exclamation point. The backslash does . (8.15 ) If you want to be convinced, experiment with some commands like: %
The first echo
command shows that the shell performs history
substitution (i.e., replaces Let's look at another alias. We want to pipe the output of ls -l into more (25.3 ) . In this case, we would want all the arguments from the command line instead of merely the last argument (or the only argument). Here's the alias: alias lm 'ls -l \!* | more'
This time, we needed both kinds of quoting:
A backslash prevents the shell from interpreting the exclamation point
immediately.
Single quotes protect the pipe symbol and the asterisk ( % Because the backslash temporarily stops the special meaning of the
Finally, if you want to pick one argument from the command line, use
This alias has two arguments: the file to which you want to add a header and the output file. When you type: %
the C shell substitutes the filename cat ~/txt/header foo > bar - , |
|