Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Why is this happening in zsh?



On Wed, Mar 14, 2012 at 20:29, Heorhi Valakhanovich <valahanovich@xxxxxx> wrote:
> This happens in many command processors. I usually use quotes like
>
> fossil --ignore "*.class"
>
> This is not a solution but usual workaround.

Actually, I would say that it is a solution, and not a workaround.

Here is an example with the find command to explain my point of view.

Let's say you want to find files in this tree:

./file1.class
./folder1/file2.class
./folder1/file2.txt
./folder2/file3.class
./folder2/file3.txt

Let see in bash:

 $ find -name *.class
./file1.class
 $ find -name *.txt
./folder2/file3.txt
./folder1/file2.txt

In zsh:
% find -name *.class
./file1.class
% find -name *.txt
zsh: no matches found: *.txt

We can see that in bash, the first command and the second command are
not doing the same thing:
"find -name *.class" is expanded to [find] [-name] [file1.class]
"find -name *.txt" is not expanted and gives [find] [-name] [*.txt]

In zsh, both lines are processed the same way:
"find -name *.class" is expanded to [find] [-name] [file1.class]
"find -name *.txt" does not match anything and returns an error.

Obviously, only the second command in bash is giving the expected
result (find all files with a given extension), in zsh you don't get
any result and the first command is not giving the expected result in
both shells.

To avoid erroneous results like the first command, the wildcard must
be escaped so that it is always sent without modification to the find
command, like this:
 $ find -name "*.class"
./file1.class
./folder2/file3.class
./folder1/file2.class

or
 $ find -name \*.class
./file1.class
./folder2/file3.class
./folder1/file2.class

Escaping the pattern ensures that the command will receive the
expected pattern, you get the following in both bash and zsh:
"find -name \*.class" is expanded to [find] [-name] [*.class]

Not escaping the pattern is a bad habit that will give unexpected
results once in a while (like the example above), so I would say that
zsh's default behavior is good because it makes people understand how
it works and avoid those mistakes.

As it was said before, it's possible to activate the broken bash
behavior in zsh, but I don't think that it's a good solution.

Regards,
-- 
Damien Thebault



Messages sorted by: Reverse Date, Date, Thread, Author