As far as I know, while using regex with grep single quotes should be used otherwise the shell interpretet it as glob(wildcard) characters and performs a filename expansion, but in the below example, the shell is taking it as regex even though the search string is not in quotes.

sh-4.3$ ls                                                                                                                                                                      
main.sh  test1  test2  test3.sh  test34  test3a                                                                                                                                 
sh-4.3$ ls | grep test3y*                                                                                                                                                       

I couldn't get the reason as to why this is happening.


That works because your expression test3y* doesn't match any filenames in the directory, so it isn't expanded into anything and is passed to grep literally making the regex valid (y* = y may occur zero or more times in that position).

You should still always quote your regex though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.