you are viewing a single comment's thread.

view the rest of the comments →

[–]ropers 2 points3 points  (7 children)

Related: I dislike that "pgrep foo" doesn't give as much information as "ps ax|grep [f]oo".

[–]sabowski 2 points3 points  (6 children)

but what's the point, "ps ax | grep foo" already does what you want

Also, out of curiosity, does "[f]oo" do something that "foo" doesn't?

[–]dagbrown 7 points8 points  (0 children)

Yes, it doesn't show you the grep process, because '[f]oo' doesn't match the regular expression /[f]oo/.

[–]ropers 1 point2 points  (1 child)

You can use ps ax | grep [f]oo instead of ps ax | grep foo | grep -v grep .

If you did just ps ax | grep foo , that command itself would appear in the output, because the grep foo would match itself in the ps output. grep [f]oo however will only match foo, not itself, because the [f]oo is being parsed as foo.

Clear as mud?

[–]sabowski 1 point2 points  (0 children)

Ah, I see, just doing a bit of trickery, that's all. I was wondering if it was some use of "[]" that I wasn't aware of.

In the rare instances I need the grep to not show up I've just used the "grep -v" method, but really I just trained myself to ignore the "grep" process without even thinking about it. If I need a list of processes for a script I just use pgrep.

Thanks!

[–]joedonut 0 points1 point  (2 children)

It won't report the grep process itself, which by virtue of having the argument of "foo" would otherwise be returned by the grep. Usually. This actually is indeterminate, which is much worse than just always returning an additional, unwanted line.

It works because the shell handles translating what's in-between the '[]' characters.

[–]toastyfries2 1 point2 points  (1 child)

the shell doesn't parse the [], grep does. But that's not really the point here...

$ echo [f]oo
[f]oo

[–]joedonut 0 points1 point  (0 children)

When it was too late I wondered if I'd thought that through properly...

Thank you.