0

Recently I found that using grep with/without quotes return different results.


grep [a-z] test

return

insert something


while

grep '[a-z]' test

return

Hello

insert something

good bye

#!/binbash

So I got confused what's the difference between these two usage?

the PATTERN MUST to be quoted?

admin
  • 23
  • 3

1 Answers1

0

The shell interprets [a-z] first if it is not quoted, before ever passing it to grep.

[a-z] is expanded to all the filenames found in the working directory that consist of exactly one letter between a and z.

I'll bet if you do a ls you will find a file (in the current directory) with the name t, or some other letter that is only present in "insert something" but not in the other strings (lines of text). If in your working directory you have several files, but exactly one has a one-letter name like t, then your first command is expanded to grep t test.

So - yes, you must quote your regular expression, so it is passed as is to grep and it is not processed by the shell.

  • yes. As you said I found a 't' named file in the dir. – admin Apr 08 '20 at 02:11
  • @admin - hehe, that was a guess; it could have been **`m`** too (another letter that is present in that line but not in the other three). –  Apr 08 '20 at 02:40