weird csh thing

Eric M. Boehm boehme at unvax.union.edu
Tue Jan 8 01:27:32 AEST 1991


bagchi at eecs.umich.edu (Ranjan Bagchi) writes:

>$ set p = "w | tail +3 "
>$ echo $p
>w | tail +3
>$ $p
>Usage: w [ -hlsuw ] [ user ]
>$ w | tail +3
>benjo    ttyp3    10:13pm           41      4  -sh
>benjo    ttyp4    10:17pm         3:22      4  -csh
>$ exit
>$ exit

>shouldn't "$p" have the same effect as "w | tail +3" ?

No, it should not. It has to do with the way the C Shell parses the
command line. The following is taken from "The UNIX C Shell Field
Guide, Gail Anderson, Paul Anderson, Prentice Hall, 1986, ISBN
0-13-937468-X", pages 262-266. (I highly recommend it).

1. History substitution - i.e.,interpret ! and ^.
2. Finding words - character strings delimited by blanks or tabs.
   Treat special characters &, |, ;, >, <, (, ), &&, ||, >>, << as words
   unless quoted.
3. Update the history list - after breaking command into words, put on
   history list.
4. Parse sequence of words in the following order:
   a. Quoting with ' and "
   b. Alias substitution
   c. I/O redirection, background execution, and pipes (recognized but
      not processed)
   d. Variable substitution
   e. Command substitution
   f. Filename expansion
5. Execute each command

Relevant to your example:
% set out = '>> ~/vault/words'
% cat dict1 >> ~/vault/words # try to replace with the following
% cat dict1 $out
[contents of dict1 appear here]
cat: cannot open >>

"What went wrong? We wanted the C shell to expand $out to specify
redirection. Unfortunately, since it has already handled redirection,
it simply passes >> to the cat command as a filename argument. Note
the importance of knowing precedence here."

"The C shell executes a subshell to process command substitution; that
is, the instance of the C shell that parses the command substitution
is distinct from the one that evaluates the text inside command
substitution marks."

In your case, since the shell has already handled redirection, it
passes | to w as an argument.
--
Eric M. Boehm
boehme at unvax.Union.EDU
BOEHME at UNION.BITNET



More information about the Comp.unix.shell mailing list