[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#26005: bug in grep

From: Eric Blake
Subject: bug#26005: bug in grep
Date: Mon, 6 Mar 2017 16:44:01 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0

tag 26005 notabug

On 03/06/2017 04:31 PM, Ildar Rakhmanov wrote:
> Hi, I tested this with latest grep 3.0 in linux RHEL and some Ubuntu
> servers as well.
> To reproduce run in empty directory
> echo "start_string_20091210.end" | grep -o
> [1-2][0][0-1][0-9][0-1][0-9][0-3][0-9]

Underquoted.  This asks the shell to perform globbing for all files that
match those patterns.  Initially, none do, so the shell passes the
pattern on unchanged to grep.

> mkdir 20011209

Now the glob succeeds, so you are invoking

echo ... | grep -o 20011209

which is NOT what you meant (it searches for the regex 20011209 in the
input, which is not present).

> echo "start_string_20091210.end" | grep -o
> [1-2][0][0-1][0-9][0-1][0-9][0-3][0-9]
> rmdir 20011209

For even more fun, try:

touch 20011209 20011210

now your grep will also output nothing.  Why? because you end up invoking:

echo ... | grep -o 20011209 20011210

which completely ignores stdin and searches for the string 20011209 in
the (empty) file 20011210.

If you had typed:

echo ... | grep -o '[1-2][0][0-1][0-9][0-1][0-9][0-3][0-9]'

then you would be immune from shell globbing changing the parameters
based on the contents of the current directory.

As the problem is in your improper use of shell quoting, and not in
grep, I'm closing this as not a bug. However, feel free to respond if
you still have questions.

Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]