Quote:
Originally Posted by GNMIKE
Commands and results:
$ ls /mydir/ | grep *
<-- (q1) I get nothing - OK
$ ls /mydir/ | grep a*c
ABC_GP0.ctl <-- (q2) why? the case is different isn't it? where are the rest?
The last example has already been explained so I'll do these two.
Unfortunately the '*' charcter is used at many levels and the first is when your shell interprets it before building the actual command line to execute. it will always do this unless you escape the * using a back slash character. (As pointed out by Perderabo)
So in your first case. What happens its your shell expands * to be all the files in your current working directory (separated by spaces). So what you are actually running is something like
ls /mydir/ | grep 'file1 file2 file3 file4'
which of course doesnt work.
In the second example the shell tries to expand the 'a*c' to match any file (again in the current directory) that starts with an 'a' and ends in a 'c'. If this succeeds then you may end up running a command like:
ls /mydir/ | grep 'access.c'
Depending on how many files match the pattern 'starts with a and ends with c' in your current directory.
If *NO* files in your current directory start with 'a' and end with 'c' then the string 'a*c' gets passed to grep. Now to grep the '*' means something slightly different. It means 'zero or more of the previously matched class'. In this case you end up greping for zero or more a's followed by one c. Hence the output you see. (Its the 'c' in ctl thats being matched, not the C in ABC).
The explanation for the 3rd example is the same but for 'A' and 'C' if you take out the extra 'ls' from the command line :-)