LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-23-2013, 02:28 AM   #1
Grtyop
Member
 
Registered: May 2013
Location: Over the Rainbow..
Distribution: LinuxMint , RedHat
Posts: 31

Rep: Reputation: Disabled
grep metacharacter question.


Click image for larger version

Name:	linuxgrep1.PNG
Views:	13
Size:	42.7 KB
ID:	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.
 
Old 12-23-2013, 02:43 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
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!
 
1 members found this post helpful.
Old 12-23-2013, 05:28 AM   #3
Grtyop
Member
 
Registered: May 2013
Location: Over the Rainbow..
Distribution: LinuxMint , RedHat
Posts: 31

Original Poster
Rep: Reputation: Disabled
Thanks alot. I totally forgotten that bash shell will expand some of those characters..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed end-of-line metacharacter $ not working qajaq Linux - Software 4 02-24-2013 10:31 PM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
[SOLVED] how can backslash be executed as a metacharacter within single quotes? joham34 Programming 4 05-06-2011 01:52 PM
question about grep new_2_unix Linux - Newbie 5 12-07-2007 04:45 PM
bash script, metacharacter protection sphynx Programming 4 04-22-2004 10:01 AM


All times are GMT -5. The time now is 10:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration