It is currently Tue, 17 May 2022 17:47:15 GMT

Author Message
 grep [f]oo and grep foo differences

    Why is that? :

ps aux | grep foo

gives me

and grep foo


ps aux | grep [f]oo
gives me only foo ?!

My shell is BASH. Regards.

    Grzegorz Goryszewski

Grzegorz Goryszewski

 Tue, 03 Jun 2003 23:11:37 GMT   
 grep [f]oo and grep foo differences

The letters inside the square brackets denote
a set of possibilities for that character.

So if you searched using:

grep [fb]oo

grep will match on foo or boo.

As you are using just [f]oo, grep matches
on foo. In effect the brackets are removed.
Therefore the grep process in the output of
ps will not be matched as that is listed as
[f]oo not foo.

I hope that makes sense,


 Tue, 03 Jun 2003 23:17:22 GMT   
 grep [f]oo and grep foo differences

The regular expression "[f]oo" is the same as the regular expression
"foo".  So they both match the same strings.  However, when you grep
with "[f]oo" your regular expression no longer matches its representation
(the regular expression comparison stops when it finds the ] after the f).
So using [f]oo instead of foo when grepping the output of ps will keep
your grep from matching on itself.

 Tue, 03 Jun 2003 23:18:39 GMT   
   [ 3 post ] 

Similar Threads

1. echo foo |grep foo && exit || wrong

2. tail | grep | grep | grep

3. grep sTerm * | grep -v grep ???

4. What is truncating the columns: ps aux | grep foo

5. "tail -f foo | grep 1 | cat"

6. What is truncating the columns: ps aux | grep foo

7. grep foo AND bar ?

8. Bug in grep or man grep

9. PLEASE HELP A NEWBIE: grep doesn't grep

10. grep question New to grep

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software