LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep metacharacter question. (https://www.linuxquestions.org/questions/linux-newbie-8/grep-metacharacter-question-4175488918/)

Grtyop 12-23-2013 02:28 AM

grep metacharacter question.
 
1 Attachment(s)
Attachment 14302

Why does ls | grep sta* not display any result ?

I don't understand how ls | grep sta*. works as well.
From my understanding, it matches each literal s-t-a, matches 'a' literal zero or more times, and matches any character. Why is the match stat as shown in the screenshot ?

Thanks.

druuna 12-23-2013 02:43 AM

You need to put the search pattern between single or double quotes (or escape the *). If you do not do this the shell will expand sta* to status1 status2. This is done before control is handed over to the rest of the command, the command becomes
Code:

ls | grep stautus1 status2
and that will fail.

These will work:
Code:

ls | grep "sta*"
ls | grep 'sta*'
# or
ls | grep sta\*

Do make it a habit to put quotes around the search pattern!

Grtyop 12-23-2013 05:28 AM

Thanks alot. I totally forgotten that bash shell will expand some of those characters..


All times are GMT -5. The time now is 09:29 AM.