I am using grep for some time,but there is some question when I use grep. when I use ‘grep’ to search some string on nginx error log,I try two ways: one:
grep 404.* error.log
for this,there is nothing find
two:
grep '404.*' error.log
for this, there can find lines that contain 404 in error.log
in my example, ‘404.*’ is the pattern,based on the result,I think when the pattern with single or double quote,the wildcard in the pattern can work, if no quote, the wildcard just treated as literal string. is it right?
Advertisement
Answer
No, without the quotes, the shell will treat it as a glob pattern and expand it itself if there are any filenames starting with 404.
in the current directory:
~ $ mkdir newdir ~ $ cd newdir ~/newdir $ touch 404.foo 404.bar somethingelse ~/newdir $ ls 404.bar 404.foo somethingelse ~/newdir $ echo 404.* 404.bar 404.foo
This is not what you want when you’re using grep, so you should always quote the search pattern.
~/newdir $ echo '404.*' 404.*